Infinite Iterations of GetChildren in a model

I’m hoping to get all children of a model, let’s say my model looks something like this:
image

Let’s just say I don’t know the inside of the model I just need to keep going down in the directory until it’s empty or I reach a part

So when I am looping over it, I can do something to it.

for iteration,part in ipairs(Model:GetChildren()) do
    -- similar to this, however i need every single child in the model (including grandchildren, etc)
end
1 Like

Use :GetDescendants() to get every descendant.

2 Likes

Can’t

You
Just

Use

GetDescendants() instead & check if the value is a Model object? That gets every known object inside what you’re looking for

for _, ModelCheck in pairs(Model:GetDescendants()) do
    if ModelCheck:IsA("Model") then
        --Do stuff here
    end
end
1 Like

I presume this flattens the hierarchy, would that include grandchildren & further down than that?

If you implement a IsA() function check, then yes you’d also get the children of all the objects of those as well in that conditional check

I’m needing to keep my script still working even if it then goes down another layer, any way to make this continue to go down the tree until it is finished getting all children & decendents.

I need the entire family tree flattened.

Would the best solution be a custom one?

Where I just make a function that flattens where I currently am, if it comes across a model or folder, it’ll call the function again to flatten it and add it to a main table.

Where once it is completed I can loop over that table?

Or is there a way within Roblox that I can do something similar to that already?

I mean maybe you could use a lot of nested loops, but I’m not sure if that would be the best approach

Can’t you just also check if what you’re looking for is a Model or a Folder? If you’re adding something to a table using that, it’d only add that specific object but not the entire descendant hierarchy I believe

(Ok why did I get a reply from here THAT WAS 7 MINUTES AGO)

I’ve made my own function, thanks for the help though.

local finalTree = {}

function flattenTree(Model)
	for iteration, child in ipairs(Model:GetDescendants()) do
		if child:IsA("Model") or child:IsA("Folder") then
			flattenTree(child)
		end
		table.insert(finalTree, child)
	end
	return finalTree
end

print(flattenTree(Bench))
finalTree = {}

That’s what GetDescendants does. I think that’s your answer.

@Jackscarlett already said this, but just to re-iterate:

for _, child in pairs(topLevelObject:GetDescendants()) do
  print(child.Name)
end

Would print children, grandchildren, great-grandchildren, etc.

Your custom flattenTree function will duplicate a lot of objects in the finalTree table.

1 Like

Actually, you’re right.

It must’ve been the way I was implementing it in my code, I was forgetting to check if the thing I was attempting to use was a Part.

Thanks, @nicemike40.

Please give @Jackscarlett or @Aeventy the solution mark, since I just repeated what they said :slight_smile: