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

How to find all children of a model?

i know this question is very simple but im new, how do i find all children, for example

LOL = script.Parent.DaModel.FindAllChildren --idk i just put FindAllChildren because im a noob
LOL.visibility = 1

4 answers

✓ Accepted answer
local Model = script.Parent.DaModel
local DaModelChildren = DaMode:GetChildren()
Model = game.Model
Children = Model:GetChildren()

Edit: Looks like the rblx devs added a function find all children late last year: https://wiki.roblox.com/index.php?title=API:Class/Instance/GetDescendants The function below is the old way of doing it, just use GetDecendants.

Original Answer: Good question, this isn't an obvious thing. To get all of them, I use a recursive function, which is a fancy way of saying that it calls itself over and over.

function getAllChildren(o)
	local foundObjects = {}
	for i,v in pairs(o:GetChildren()) do
		table.insert(foundObjects, v)
		for j,k in pairs(getAllChildren(v)) do
			table.insert(foundObjects, k)
		end
	end
	return foundObjects
end

I'll just leave the code here, in case anyone else needs it.

2 comments

0

Nice! I wasn't aware they'd added this functionality, but I'm glad to see the devs finally got around to it!

There is a function called :GetDescendants(). http://wiki.roblox.com/index.php?title=API:Class/Instance/GetDescendants. Since I don't support leechers I won't be fixing your code but merely providing an example.

for i,v in pairs (game.Workspace:GetDescendants()) do --Gets the descendants using iPairs
print(v) --Prints the descendants
end--ends

Please accept answer if this helped! Again, I do not like the leechers that don't accept answers.

1 comment

0

ummmm, wouldnt this give me all the childrens in workspace? and, just for you to know, im not being a leecher because not accepting your answer, other people answered too

This discussion is preserved as it was. New answers, comments, and votes are closed.