Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to clear everything inside a frame/part?

Asked by 4 years ago

I'm working on a playerlist (The Playerlist is displayed on a part). It display the name of every player in the game. I want to "delete" every Name GUI after 20 seconds but it only destroys the first GUI and not all the other one. I tried it with repeat and wait methods but nothing works.

local BoardList = game:GetService("ReplicatedStorage"):WaitForChild("BoardList")
local holder = script.Parent:WaitForChild("holder"):WaitForChild("ScrollingFrame")

while true do
wait(5)
Players = game:GetService("Players")
for i, player in pairs(Players:GetPlayers()) do
local new = BoardList:Clone()
    new.name.Text = player.Name
    if not new.name.TextFits then
        new.name.TextScaled = true
end
    new.Parent = holder
end
local ListOfPlayers = script.Parent.holder.ScrollingFrame.BoardList
wait(20)
  ListOfPlayers:Destroy()
end

2 answers

Log in to vote
0
Answered by
aredanks 117
4 years ago

Your script in correlation to your question title is somewhat confusing so I will just go according to your title. Otherwise if you're not deleting individual GUI objects, just delete the entire frame its in instead. So to answer your question:

Assume playerlist is defined so in your case I will assume is:

local playerlistFrame = script.Parent.Frame
-- I am just guessing, it might be script.Parent.holder.ScrollingFrame.BoardList but I can't tell

Now there are two methods to doing this depending on your use case. If you are simply removing everything inside of it as in their children which is only the hierarchy below the frame:

for _, v in pairs(playerlistFrame:GetChildren()) do
    v:Destroy()
end

If you are only targeting GUI then you will need to use an if statement since it will target all children including scripts and UI constraints. To do that with the if statement you can search for the class "GuiObject".

for _, v in pairs(playerlistFrame:GetChildren()) do
    if v:IsA("GuiObject") then -- includes frames, textbuttons, whatever else related
        v:Destroy()
    end
end

The second method, if you have more than just children and those children have children as well, you can use :GetDescendants() which removes entirely everything inside it. You can just replace GetChildren() with GetDescendants() and that's it.

for _, v in pairs(playerlistFrame:GetDescendants()) do
    if v:IsA("GuiObject") then
        v:Destroy()
    end
end

Since I am assuming you don't know about placeholder variables as you have a unused i variable, you can use the underscore _ to replace useless variables in the pairs loop. For example in your script, I would replace i with _ since you won't use i.

Note: In this use case, pairs or ipairs does not matter by choice but will matter in different scenarios, usually involving tables.

0
Mhm. I'm actually new to LUA and I'm still learning! Thank you so much it's working now :D DeAuroraxx 50 — 4y
0
No problem. aredanks 117 — 4y
Ad
Log in to vote
0
Answered by
DrShockz 233 Moderation Voter
4 years ago

Using a for loop is the most efficient for this method here is an example!

local playerlist = script.Parent.Frame:GetChildren()

for _,gui in pairs(playerlist) do
 if gui:IsA("ScrollingFrame") then
 gui:Destroy()
 end
end

This script will destroy all of the children in the frame that has is a "ScrollingFrame"

Answer this question