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

How would I scan all the children of a Model?

I know that for i,v in pairs (Model:GetChildren()) will return all the children of a model. But what if there are models inside that model? I don't want to have to do

for i,v in pairs (Model:GetChildren()) do
if v:IsA("Model") then
for i,v in pairs (v:GetChildren()) do
if v:IsA("Model") then
for i,v in pairs (v:GetChildren()) do
end
end
end

So is there any easier way to scan ALL the children of a model, including children inside models inside that model?

2 comments

0

Recursion

0

The term for "children of children of ..." is "descendant"

1 answer

✓ Accepted answer

As JetCrusher said, recursion.

You can call a function inside a function, this will prove useful for the process.

function scan(item) --function right here, item is the item being scanned.
--Stuff here.
if #item:GetChildren() > 0 then --if anything is in it, then do the next thing.
for _,v in pairs(item:GetChildren()) do --Loop through objects or descendants of 'item'.
scan(v) --This will scan the objects inside of 'item'
end --end loop
end --end if then
end --end function

for _,v in pairs(workspace:GetChildren()) do --Go through all workspace elements.
scan(v) --v will be scanned
end

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