Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I make this script more efficient if Possible?

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)

4 comments

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.

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.

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.

0

@M39a9am3R Thank you for that i never checked

1 answer

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

1 comment

0

Thanks mate gave me alot to think about, also thanks for answering this!

This discussion is preserved as it was. New answers, comments, and votes are closed.