ClearAllChildrenOfClass / ClearAllDescendantsOfClass

When working with GUIs specifically with ScrollingFrames, I almost always use a UIConstaint or UILayout, and sometimes I need to clear out GuiObjects and not UIComponents.

adding ClearAllChildrenOfClass / ClearAllDescendantsOfClass will significantly help with this use case where we need to target and remove specific classes from Instances


For example when we need to remove ParticleEmitter, Lights, Smoke, Sparkles, Trail, Decal, Beam, Texture from a Part / Character after a VFX is finished.

without ClearAllChildrenOfClass / ClearAllDescendantsOfClass it is difficult to remove Effects only while keeping other essential constructors like Motor6D, Welds, Attachments, etc

Specific cases for UIs:

  • when we need to display a list of items as pages and reuse the same Frame / ScrollingFrame
  • when we need to display an inventory and reuse the same Frame / ScrollingFrame
12 Likes

“I need X because I want to do X” is not a great example of a use case. You might want to refer to How to post a Feature Request and clarify more about the problem you’re trying to solve (e.g. what made you end up in a situation where you need to clear a bunch of children of a certain type?) + title your thread after a problem rather than a proposed solution (API signature). There may be more fitting/generic solutions one could think of if the underlying problem is more clear.

thanks for the reminder, I have edited the thread let me know if something is still missing

1 Like

For your use case, have you tried using the following script:

local children = script.Parent:GetChildren()
for _, child in pairs(children) do
	if child:isA("GuiObject") then
		child:Destroy()
	end
end
2 Likes

Yes, I have used two methods

One is :ClearAllChildren then clone in a new set of UIComponents
and the other method is the one you provided

and I got this dm attaching a script from @SillyMeTimbers that provide a more general method that you can use:

function ClearAllChildrenOfClass(parent, className)
    for i, ClearInstance in pairs(parent:GetChildren()) do
        if className ~= nil and type(className) == "string" and ClearInstance:IsA(className) then
            ClearInstance:Destroy()
        end
    end
end

function ClearAllDescendantsOfClass(parent, className)
    for i, ClearInstance in pairs(parent:GetDescendants()) do
        if className ~= nil and type(className) == "string" and ClearInstance:IsA(className) then
            ClearInstance:Destroy()
        end
    end
end

Hope those would be helpful.

That code is written with bad practice, it does pre-condition checks each iteration and for some reason allows the function to run at all with invalid className, and checks className but not parent. Also when iterating arrays, ipairs should be used for clarity, and you can use _ in place of unused vars.

For future readers, this is the production version of above:

function ClearAllChildrenOfClass(parent, className)
	assert(typeof(parent) == "Instance", "parent is not an Instance")
	assert(typeof(className) == "string", "className is not a string")

    for _, child in ipairs(parent:GetChildren()) do
        if child:IsA(className) then
            child:Destroy()
        end
    end
end

function ClearAllDescendantsOfClass(parent, className)
	assert(typeof(parent) == "Instance", "parent is not an Instance")
	assert(typeof(className) == "string", "className is not a string")

    for _, descendant in ipairs(parent:GetDescendants()) do
        if descendant:IsA(className) then
            descendant:Destroy()
        end
    end
end

Or more succinctly with Luau type annotations:

function ClearAllChildrenOfClass(parent: Instance, className: string)
    for _, child in ipairs(parent:GetChildren()) do
        if child:IsA(className) then
            child:Destroy()
        end
    end
end

function ClearAllDescendantsOfClass(parent: Instance, className: string)
    for _, descendant in ipairs(parent:GetDescendants()) do
        if descendant:IsA(className) then
            descendant:Destroy()
        end
    end
end
13 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.