Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

[Solved :3]What's the difference of "local function" and "function"?

Asked by 4 years ago
Edited 4 years ago

So basically I'm very curious why some people do this when creating a function

local function Eat()
    Food.Parent = Enum.Organs.Stomach
end

instead, why don't they do it like this?

function Eat()
    Food.Parent = Enum.Organs.Stomach
end

My point is why do some people use local function instead of function? I mean it does the same but why type the word "local" before typing the word "function" if it does the same thing?

1 answer

Log in to vote
3
Answered by
CjayPlyz 643 Moderation Voter
4 years ago
Edited 4 years ago

I'll try to explain in the most simplest of examples.

you can only call local function after you declare it:

local function myfunction ()
    --script
end

myfunction()

it will not work if you try to call it before it's declared:

myfunction()

local function myfunction ()
    --script
end

however if the the local function is inside another function or local function. you can only call that local function inside that function or local function:

local function firstfunction ()
    local function secondfunction ()
        --script
    end

    secondfunction()
end

things like this will not work:

local function firstfunction ()
    secondfunction()

    local function secondfunction ()
        --script
    end
end

or

local function firstfunction ()
    local function secondfunction ()
        --script
    end
end

secondfunction()

or

secondfunction()

local function firstfunction ()
    local function secondfunction ()
        --script
    end
end

Now, global function (functions without local)

You can call a global function anywhere as long as it is in the same script

secondfunction()

function firstfunction ()
    local function secondfunction ()
        --script
    end
end

or

local firstfunction ()
    function secondfunction ()
        --script
    end
end

secondfunction()

There might be things I said wrong, so check these if you want to learn more.

https://developer.roblox.com/en-us/articles/Variables

https://roblox.fandom.com/wiki/Function

https://developer.roblox.com/en-us/articles/Scope

1
Thank you so much for you're answer! DizzyGuy70 38 — 4y
1
Well explained. Thank you. ImTrev 344 — 4y
Ad

Answer this question