Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to remove all instances of GetChildren()?

Hello im wondering on how i would get rid of all instances of get children im making a sort of cleaning script.

i tried using :Remove() and :Destroy() but it popped up with an error

3 answers

local object = workspace

for i,v in pairs(object:GetChildren()) do
	v:Destroy()
end

Now this deletes everything in the specified object. If you want to delete a specific type of class then you would need to specify that as well using the IsA method that returns whether an instance is a specific class.

An example would be:

local object = workspace

for i,v in pairs(object:GetChildren()) do
	if v:IsA("Hat") then
		v:Destroy()
	end
end

Read more:

IsA

Class Reference

3 comments

0

How could you know that. I thought you just fm.

0

Any proof of that?

0

just saying i think this guy knows more

Try setting the variable that you used to nil, I believe once nothing is referencing the list, it will disappear; I've never heard of need to do manual garbage collection in Roblox.

list = Model:GetChildren()
for...end
list = nil

Also, if you're really worried about script/game speed use local variables if you can.

---Edit, after some comment discussion

If you want to remove all items in a list, which is what you get from GetChildren() then use a for loop to look through it

list = Model:GetChildren()
for i=1, #list do
	list[i]:Remove()
end

5 comments

0

what i have currently is 3 cards get generated. but when i hit Replay the cards stay.. i need a script that will remove the cards each card is named "Card" so i assumed getchildren would be the easiest way

0

GetChildren returns a list; you're looking for how to get rid of each item in a list

0

Thank you i will try this

0

Still not working :(

0

nvm... im stupid

local items = game:GetService("Workspace").Model

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

That should work just fine.

This discussion is preserved as it was. New answers, comments, and votes are closed.