✓ Accepted answer
If you know that all of the scripts are precisely the children of children, you could just write a nested loop:
for _, child in pairs(model:GetChildren()) do
for _, grandchild in pairs(child:GetChildren()) do
if grandchild:IsA("Script") then
grandchild.Disabled = true
end
end
end
Of course, if you don't know that (and probably you don't / shouldn't confine yourself to that) then you should turn to recursion.
A descendant of a model is either a child or a descendant of a child. With that recursive definition we can define something to make a list of all descendants:
function descendants( model )
local allDescendants = {}
for _, child in pairs(model:GetChildren()) do
table.insert(allDescendants, child)
for _, childdesc in pairs( descendants(child) ) do
table.insert(allDescendants, childdesc)
end
end
return allDescendants
end
for _, descendant in pairs(descedannts(model)) do
if descendant:IsA("Script") then
descendant.Disabled = true
end
end
Or, just wrap the modification into the recursive function and avoid adding to tables (though this makes the descendants function not reusable):
function disableScripts( model )
if model:IsA("Script") then
model.Disabled = true
end
for _, child in pairs(model:GetChildren()) do
disableScripts(child)
end
end
1 comment
Are you sure there isn't a way similar to the example I gave? If not, how do I iterate over all children as well as the original