ClearAllChildren is dangerous

If something parents things on ChildRemoved and ClearAllChildren is called on it, the server will crash.

x=Instance.new("Part",workspace)
Instance.new("IntValue",x)
x.ChildRemoved:connect(function()Instance.new("IntValue",x)end)
x:ClearAllChildren()

I think x:ClearAllChildren() should behave like for _,v in pairs(x:GetChildren())do v:Destroy()end instead of repeat x:GetChildren()[1]:Destroy()until #x:GetChildren()==0 so it just destroys its current children instead of destroying one at a time until there aren’t any left.

found this issue in a script builder where I tried running ClearAllChildren on my character while it automatically regenerates.

2 Likes

It probably is something similar to this in Lua:

local tab = {"a","b","c"}
for k,v in pairs(tab) do
tab[k] = nil
-- ChildRemoved: table.insert(tab,"hi")
end

Since something gets added to the array(list-thing) holding the children ClearAllChildren is going over.

Nice to know to watch out for that, though.

how can I avoid crashing the server when someone calls ClearAllChildren on my character/torso but limbs and joints are automatically being created and added back on ChildRemoved?

Eh, wait() and completely respawn?

completely respawn is the thing I am trying to avoid ever needing to do.

They might as well do Character:Destroy()

and that’s the only case.