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

How to disable all scripts that are a descendant of a model?

Asked by 9 years ago

So I have this hierarchy for instance:

ModelA
 ScriptA
 ScriptB
 ModelB
  ScriptC
  ScriptD
 PartA
  ScriptE
ScriptF

How would I disable every script inside ModelA (A,B,C,D,E) but not the one outside it (F)? To be clear I don't want to have to individually target all of them, I want something like:

ModelA:Find("Script").Enabled = false

Is there any way to accomplish this?

2 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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) -- Children are descendants
        for _, childdesc in pairs( descendants(child) ) do
            table.insert(allDescendants, childdesc)
            -- Descendants of children are descendants of model
        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
Ad
Log in to vote
-1
Answered by 9 years ago

Use a loop to iterate over each item and if the value is == to the script, then set it to false.

0
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 Norshine 88 — 9y

Answer this question