Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I scan all the children of a Model?

Asked by
Zerio920 285 Moderation Voter
10 years ago

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?

0
Recursion User#2263 0 — 10y
0
The term for "children of children of ..." is "descendant" BlueTaslem 18071 — 10y

1 answer

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
10 years ago

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
Ad

Answer this question