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.

45 Likes

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

16 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.

10 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
30 Likes

Bumping, please add. Bringing this up because a C++ implementation would be a lot faster than looping over hundreds/thousands of instances with if statements.

This would’ve been insanely useful in the Audio Discovery plugin which scans the entire DataModel for audio instances and does it on the Lua side, I’m sure performance would’ve been gained if it had an Instance method.

I’m requesting this for performance, not to save writing code in a util module, sure a lot of performance optimisations could be made if this had a C implementation.

13 Likes