for-loop - How to remove all cloned gui in roblox
问题描述
I am creating a dance show game and I want to show the players name and their sequence in a list before each round. I am using clone which creates a new label for each player. My issue is that the cloned list is not getting removed after round 1 and the players name are duplicated. Here is my code
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)
解决方案
Your problem is that when you are cleaning things up, the nameList
and seqno
variables are only pointing at the last items you added to the list, it is not removing all of the items that you added.
There are a few different ways you could go about removing them, but the one that I will recommend will be to
- Create an empty container like a Frame.
- When creating a new TextLabel, put it as a child of that Frame.
- When you are ready to clean up, just delete the Frame and that will remove all the children at once.
That would look like this :
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)
推荐阅读
- google-chrome - Jenkins 中的 TestCafe 无法建立浏览器连接
- javascript - 如何使用不直接导出的组件?
- asp.net - 在调用 HttpRequest.ValidateString() 之前修改 asp 文本框
- angular - prime-ng 下拉菜单上的 prime-ng 工具提示
- angular - 在 Map 之前按嵌套数组中的值排序
- php - 使用 Node 或 PHP 使用 SOAP WS-Security
- for-loop - 重置循环变量
- r - 在 R 中使用具有 HVDM 度量的 distances() 函数计算对之间的距离
- python - 如何防止句子在csv中被分成几行?
- python - 将元组与多个项目python合并