As always, I see things on the tutorials with “Local function”. I get very confused about how they work, however, it does something than the normal function. What could it possibly it be?
Local functions can only be used in one script, while normal functions (A.K.A. “Global Functions”) can be used from any script in the game.
Edit: see below, better explanation
Just to give you an idea:
function foo()
print("Foo.")
end
is exactly the same as
foo = function()
print("Foo.")
end
and
local function foo()
print("Foo.")
end
is exactly the same as
local foo = function()
print("Foo.")
end
** with a small difference:
The first (function foo()
) makes foo a global variable, and the other (local function foo()
) makes foo a local variable. With that being said, as with any other variable, you should always declare functions as local.
I think the better term is “function environment”. For example, I can’t put a function inside a function and try to obtain it outside of the parent function even though it’s in the same script.
Note: I wonder if a global function inside of a local function is still globally accessible.
These two specifically are not exactly the same; they have a tiny difference in their scopes. Within the one defined as local function
, foo
(its name) can be accessed as an upvalue for recursion, while in the local foo =
definition, it can not.
Makes sense. To give a concrete example:
You can do:
local function foo()
foo()
end
But you can’t do:
local foo = function()
foo()
end
You’re mixing up global functions and functions that are inside a shared table (_G
or shared
). Global functions are just when you do function something()
instead of local function something()
.
For the sake of completeness…
local function foo()
print("Foo.")
foo()
end
is exactly equivalent to
local foo
foo = function()
print("Foo.")
foo()
end
Does that mean you can require
any script to make it happen?
I heard calling local functions inside a local function is bad. is that True? Example:
local OnSpawn = function(Character)
end
local OnAdded = function(Player)
Player.CharacterAdded:Connect(OnSpawn)
end
game.Players.PlayerAdded:Connect(OnAdded)
Ima do local functions more like this.
There’s nothing particularly wrong with this code, but it might make more sense to do local function name
instead of local name = function
.
To sum, it doesn’t matter, just make them local and that’s it
local function:
- can access it after this was created. meaning put this at the top.
function:
- can access any positions in a script. meaning you can access this even it is located at the top or bottom of the script, it doesn’t matter.
==Pros and Cons==
local function:
Pros:
optimized.
Cons:
cannot access when its at the bottom.
function:
Pros: Can access anywhere in a script.
Cons: not optimize
You can’t use local functions inside a module
great explanation right here .
If you use local function consistently or you want to call it many times in a short time lets say 25x per second maybe you are collecting coins or playing UI animations then do this:
~
local function Func()
end
Event:Connect(Func)
or if you sometimes call it use this:
~
Event:Connect(function()
end)
Does it matter?
Yes.
Pros and cons. Consider this:
Using the 1st method:
Pros: You already made a code to it to run and no need to recreate it again in short period of time. meaning less calculation.
Cons: can use up more memory.
2nd method:
Pros: Less memory because you did not run the function yet.
Cons: The entire function are needed to rerun and rerun. meaning more calculation will be executed.