首页 > 解决方案 > 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)

标签: for-loopoopluacloneroblox

解决方案


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

  1. Create an empty container like a Frame.
  2. When creating a new TextLabel, put it as a child of that Frame.
  3. 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)

推荐阅读