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

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

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

✓ Accepted answer

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

2 comments

1

Thank you so much for you're answer!

1

Well explained. Thank you.

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