What is the difference between a local function and a normal function?

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?

11 Likes

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

7 Likes

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.

50 Likes

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.

3 Likes

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.

10 Likes

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
4 Likes

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

3 Likes

For the sake of completeness…

local function foo()
    print("Foo.")
    foo()
end

is exactly equivalent to

local foo
foo = function()
    print("Foo.")
    foo()
end
8 Likes

Does that mean you can require any script to make it happen?

2 Likes

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.

3 Likes

To sum, it doesn’t matter, just make them local and that’s it

1 Like

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

1 Like