Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to Find all Children of an Instance by Class?

Asked by 4 years ago

We do have :FindFirstChildOfClass() but It will only choose one... If I'm wrong please tell me. I want to know if there is a way to find all children from one class.

2 answers

Log in to vote
3
Answered by
Norbunny 555 Moderation Voter
4 years ago
Edited 4 years ago

That I'm aware of, there is no built-in function to do it. The best way to get all children by a class is writing a function for it yourself, which is fairly simple!

Loop through the instance's children, check their class, if it matches the one you are looking for, then add it to a table. In the end, return the table.

It's fairly simple to write

local function getChildrenOfClass(instance, class)
    -- table to hold the children
    local children = {}

    -- looping through the instance's children
    for i,v in pairs(instance:GetChildren()) do

        -- check if the child's class matches the one we're looking for
        if(v.ClassName == class)) then
            -- we add it to the table
            table.insert(children, v)
        end
    end

    -- we return the table, so that it can be used when calling the function
    return children
end

Afterwards, you can call your function whenever needed, and access it!

To access it, you can do it in the same way that you would for FindFirstChildOfClass() !

-- This will allow you to use the children you've selected using the function
local childrenOfClass = getChildrenOfClass(model, "Part")

for i,v in pairs(childrenOfClass) do
    print(v.Name) -- This would output the part's name, you can do anything else with it!
end

Documentation for tables, ClassName, loops

1
Im not a pro in scripting, so could you refer to the returned thing? NathanTheCraziest 105 — 4y
0
Yep! I'll edit my answer to also answer that! Norbunny 555 — 4y
1
Thanks! NathanTheCraziest 105 — 4y
0
Happy to help! Norbunny 555 — 4y
View all comments (9 more)
0
for the "local childrenOfClass = getChildrenOfClass(model, "Part")" when is say model, "Part" do w have to change it? If so, what should we change it to. NathanTheCraziest 105 — 4y
0
Model is your Instance, but I wanted to give a practical example. "Part" is the class type, could be anything. Script, MeshPart, ShirtGraphic, anything you would like! Norbunny 555 — 4y
1
Oh ok got it! NathanTheCraziest 105 — 4y
0
Hope that this helped, if you have any further questions, let me know! Norbunny 555 — 4y
1
It worked, thanks alot! NathanTheCraziest 105 — 4y
0
Wait, how do you make it create a clone of the stuff that it finds and put in a player? NathanTheCraziest 105 — 4y
0
Using :Clone() and parenting the cloned instance to the player; https://developer.roblox.com/en-us/api-reference/function/Instance/Clone Norbunny 555 — 4y
1
Do note that IsA will return true for any object that inherits that Class too Filipalla 504 — 4y
0
My bad, will edit to use .ClassName, ty! Norbunny 555 — 4y
Ad
Log in to vote
0
Answered by
Filipalla 504 Moderation Voter
4 years ago
function GetChildrenOfClass(Instance, Class)
    local ChildrenOfClass = {}
    for _, Child in pairs(Instance:GetChildren()) do -- iterate through table returned by GetChildren
        if Child.ClassName == Class then -- Check if ClassName matches Class Argument
            table.insert(ChildrenOfClass, Child) -- insert into table
        end
    end
    return ChildrenOfClass -- Return the table
end

Answer this question