Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a function which searches through all descendants?

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!

7 comments

0

:GetChildren() returns all regardless, but there isn't a single function to return a certain class

0

Yeah I know that, but does it go further than just the child? Like the child's child?

0

Descendants? No. You're going to have to code that yourself. Sorry!

0

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.

0

Awh. I'm not that good yet .-.

0

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.

0

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.

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

0

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

0

That might work. I'll try it out.

0

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

0

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.