I’m hoping to get all children of a model, let’s say my model looks something like this:
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
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.
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 = {}