I know there's a FindFirstChild function, but I would like to know if there is a function which returns all the descendants, which I specific? [Edit] More specifically, a function which can search through a whole "family tree" and keep searching the children, and its children, etc. and returns all the children which have a specified name? If there isn't, does anyone know how to make one? Thanks!
Is there a function which searches through all descendants?
2 answers
#What you could do Use a loop to go through all the children, if there are more children, use the function to count to see if there are more children!
#Final Product
We can use :GetChildren() to get the children of the original object, then use it again to do the function again to the descendants.
Edit the following codes to your liking!
###Count Descendants:
local descendants = 0
function count(obj) --counts descendants only
if #obj:GetChildren() > 0 then
for _,v in pairs(obj:GetChildren()) do
descendants = descendants+1
count(v)
end
end
end
count(workspace.Model) --counts up the descendants
print(descendants) --Print the number of items inside of a model for example.
###Change Descendants Color's
function count(obj)
if #obj:GetChildren() > 0 then
for _,v in pairs(obj:GetChildren()) do
v.BrickColor = BrickColor.Random()
count(v)
end
end
end
color(workspace.Model) --colors all the descendants
Hope it helps!
1 comment
That's kinda good, but the only probably is it doesn't return the actually descendants.. It only counts them. However, I can use this as a base.
I think what youre looking for can be found by using loops and tables.
for i, child1 in pairs(workspace.Object:GetChildren()) do -- loops through each child of the object
print(Child1)
--OPTIONAL getting that childs child
for i, child2 in pairs(Child1:GetChildren()) do -- loops through the childs children
print(child2)
end
3 comments
That might work. I'll try it out.
The problem is that if there are more than 3 descendants it will only get up to the 2nd. And copy and pasting the same code and changing the variable's names 50+ times is not exactly the ideal way to go...
Yeah I kinda guessed this wasn't the magic code... I might try to go from this and edit it, or something.
This discussion is preserved as it was. New answers, comments, and votes are closed.

7 comments
:GetChildren() returns all regardless, but there isn't a single function to return a certain class
Yeah I know that, but does it go further than just the child? Like the child's child?
Descendants? No. You're going to have to code that yourself. Sorry!
And we can't make one unless you supply a code for us to fix, give it a try and if it doesn't work, put the code on here for us to code.
Awh. I'm not that good yet .-.
I'm not supposed to give you the actual codes. It's against the site rules. I can infact give you something to work off of. Also, accept my answer if it works. Not upvoting it.
Oh right, that's fine. But I haven't had the time to try it yet. That's why I upvoted it, for generally being helpful.