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

How can I make this script more efficient if Possible?

Asked by 8 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

Note: This is not intended to be a virus this is just a script I am working on for fun. I would just like to know if it is possible to make this run on less lines of code, this is the shortest I can think of right now. Any and all ideas are appreciated!


function Mute(object, script) if object:IsA("Script") or object:IsA("LocalScript") or object:IsA("ModuleScript") then object.Disabled = true end for _, Child in ipairs(object:GetChildren()) do Mute(Child, script) end end Mute(game.Workspace)
0
I've heard that by not using IsA() and using if object.ClassName == ClassName is more efficient. I can't promise that is true but it might be. AZDev 590 — 8y
0
@AZDev I've not tested that, nor have I heard anything like that. If it were to be inefficient, then it must not leave too much impact on the game. As for the script, I can not think of how to make it any more efficient. I can however state there is no disabled property of ModuleScripts. M39a9am3R 3210 — 8y
1
You could argue that comparing it's class name with a string is "more efficient", but micro-optimizing your code to death is much worse. Yes there is more going on behind the scenes while using a function call, but this is why the option exists. ScriptGuider 5640 — 8y
0
@M39a9am3R Thank you for that i never checked lacikaturai 89 — 8y

1 answer

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

There's not really going to be a way to make this shorter (though there's no reason for the script variable in this)

If you find yourself doing this sort of thing a lot, it may be useful to make a descendants function:

function descendants(obj, m)
    m = m or {}
    for _, child in pairs(obj) do
        table.insert(m, child)
        descendants(child, m)
    end
    return m
end

This makes your script simply

for _, child in pairs( descendants(workspace) ) do
    if child:IsA("Script") or child:IsA("LocalScript") then
        child.Disabled = false
    end
end

...if you want to add more utility functions, you could add a filter:

function filter(list, f)
    local out = {}
    for i = 1, #list do
        if f(list[i]) then
            out[#out+1] = list[i]
        end
    end
    return out
end

After those are defined, you can use something like

function isScript(obj)
    return obj:IsA("Script") or obj:IsA("LocalScript")
end

for _, child in pairs(filter(descendants(workspace), isScript)) do
    child.Disabled = true
end
0
Thanks mate gave me alot to think about, also thanks for answering this! lacikaturai 89 — 8y
Ad

Answer this question