Can you use local functions in a ModuleScript?

I have heard that local variables are faster than global variables and was wondering, does that mean local functions are faster than global functions?

Also, I have a ModuleScript for Client use so I have stored it in ReplicatedStorage. I am wondering, do you only use functions in ModuleScripts for both server-side and client-side or do you use local functions. If so when do you use local functions and global function?

LocalScript:

script.Parent.Activated:Connect(function()
    ClientModule.Activated()
end)

ModuleScript:

local ClientModule = {}

function ClientModule.Activated()
	print("Tool was activated!")
end

return ClientModule

The only difference between local and global functions are it’s scope. Technically local functions are faster than global ones (Sometimes ~10% faster).

ModuleScripts should be used if you have a specific function used in multiple scripts. You can also make an OOP system but that’s pretty advanced.


TL;DR: Use local functions if you only use that function in a single script, a ModuleScript otherwise.

1 Like

So when you say around 10% faster what do you mean? Does it have to do with performance?

Yes. I have seen ModuleScripts used in Object-Orientated Programming.

When I say ~10% faster, I mean it performs 10% faster (or even faster) whenever a localized variable or function is used in a time-critical loop. An official article references this.

You can read why a local variable or function is faster on the article above.

1 Like