Add Instance:GetChildrenOfClass and an inheritance respecting equivalent

As a Roblox developer, it is annoying to iterate through the children of an Instance that are a specific class because everything has to be wrapped in a check for its class. This comes up when designing UI, specifically procedurally generated lists and grids, as UIConstraints are used to sort these and have to be inside of the object they’re sorting the children of.

A GetChildrenOfClass method would make this much easier as the filtering would already be done and iteration would not have to be wrapped in an if-statement.

It would also be helpful to have a similiar method that uses class inheritance like IsA, though the naming convention might be a bit weird (GetChildrenWhichAre?). The use case for an inheritance respecting equivalent is the same as the GetChildrenOfClass, but it would be helpful to iterate through GuiObjects for example instead of just Frames.

21 Likes

This has regularly been an annoyance for me when developing UI. Using UI constraints requires I wrap all child iteration in class checks.

9 Likes

I mean I’d argue that it would take ~10 seconds to make a function that does this yourself but I too am lazy so I support.

4 Likes

Necroing this because It would be a great addition to the api

:GetChildrenOfClass(classname)
:GetDescendantsOfClass(classname)
:GetChildrenThatAre(classname)
:GetDescendantsThatAre(classname)

All of these would be quite nice

Usecase: You are writing a custom player spawning system and want spawn location parts to behave as they do normally without having to tag them or put them all in one folder, simply call Workspace:GetDescendantsOfClass("SpawnLocation") to get them all.
Usecase 2: You want to get every basepart under an instance to change their color you would call container:GetDescendantsThatAre("BasePart")

This addition would also simplify some common patterns

for _, instance in pairs(container:GetChildren()) do
    if instance:IsA("BasePart") then
        -- do thing
    end
end

would become:

for _, basePart in pairs(container:GetChildrenThatAre("BasePart")) do
    -- do thing
end
7 Likes