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