如何在 roblox 中删除所有克隆的 gui
Posted
技术标签:
【中文标题】如何在 roblox 中删除所有克隆的 gui【英文标题】:How to remove all cloned gui in roblox 【发布时间】:2021-04-16 04:07:50 【问题描述】:我正在创建一个舞蹈表演游戏,我想在每一轮之前在列表中显示玩家的姓名和他们的顺序。我正在使用克隆,它为每个玩家创建一个新标签。我的问题是在第 1 轮之后克隆的列表没有被删除,并且玩家的名字是重复的。这是我的代码
contList.OnClientEvent:Connect(function(Contestants) ---Remote event in local script
for i=1,#Contestants do
nameList = script.Parent.List.plrsList.nameList:Clone()
nameList.Parent = script.Parent.List.plrsList
nameList.Name = Contestants[i].Name
nameList.nList.Text = Contestants[i].Name --players name
seqNo = script.Parent.List.sequence.seqNo:Clone()
seqNo.Parent = script.Parent.List.sequence
seqNo.Name =i --sequence number
seqNo.sequence.Text = i
List = script.Parent.List
end
List.Visible = true
wait(10)
List.Visible = false
for i=1,#Contestants do
nameList:Remove()
seqNo:Remove()
end
end)
【问题讨论】:
Instance:Remove() 已弃用 developer.roblox.com/en-us/api-reference/function/Instance/… 我用了destroy也还是不行@Piglet 【参考方案1】:您的问题是,当您清理内容时,nameList
和 seqno
变量仅指向您添加到列表中的最后一项,而不是删除您添加的所有项。
您可以通过几种不同的方法来删除它们,但我推荐的一种方法是
-
创建一个像框架一样的空容器。
创建新的 TextLabel 时,将其作为该 Frame 的子项。
当您准备好清理时,只需删除 Frame 即可立即删除所有子项。
看起来像这样:
contList.OnClientEvent:Connect(function(Contestants) ---Remote event in local script
-- find some UI elements
local list = script.Parent.List
local playersList = list.plrsList
local playerNameList = playersList.nameList
local sequence = list.sequence
local sequenceNumber = sequence.seqNo
-- 1. create an empty container for all players
local container = Instance.new("Frame")
container.Size = UDim2.new(1, 0, 1, 0)
container.Position = UDim2.new(0, 0, 0, 0)
container.BackgroundTransparency = 1.0
local layout = Instance.new("UIListLayout", container)
layout.SortOrder = Enum.SortOrder.Name
-- 1a. create a container for player names
local playerContainer = container:Clone()
playerContainer.Parent = playersList
-- 1b. create a container for player numbers
local sequenceContainer = container:Clone()
sequenceContainer.Parent = sequence
-- 2. create an entry for every player, and put them into their containers
for i = 1, #Contestants, 1 do
local nameList = playerNameList:Clone()
nameList.Name = Contestants[i].Name
nameList.nList.Text = Contestants[i].Name
nameList.Parent = playerContainer
local seqNo = sequenceNumber:Clone()
seqNo.Name = tostring(i)
seqNo.sequence.Text = tostring(i)
seqNo.Parent = sequenceContainer
end
-- momentarily show the list
list.Visible = true
wait(10)
list.Visible = false
-- 3. clean up by deleting the containers
playerContainer:Destroy()
sequenceContainer:Destroy()
end)
【讨论】:
以上是关于如何在 roblox 中删除所有克隆的 gui的主要内容,如果未能解决你的问题,请参考以下文章