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)
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