I know this post exist many times, but actually I still didnt understand it. What does local do? and what is the difference between local function and function. Can someone give me a clearly explanation just for me.
pls dont just give a code. I want understand it really good.
Globalâs affect everyoneâs game, for example if you wanted to script a house with an opening door you would use global, but local is for personal servers or particular players.
That is local scripts vs Server scripts. They are asking the difference between:
local function AddNumbers()
vs
function AddNumbers()
The main difference is that you can call a global function in code above the function. So you can do this with a global function, while you couldnât do that with a local function.
print(AddNumbers(10, 20))
function AddNumbers(1, 2)
return(1 + 2)
end
This is an advanced question and the differences and benefits of using one over the other can only be appreciated when you have enough experience in lua.
The simple answer is that functions (without using the word local) are scoped throughout the code, that is, they are global no matter where they are. for example this code will print âHelloâ.
if true then
function hello()
print("Hello")
end
end
hello()
this one too:
function some()
function thing()
function hello()
print("Hello")
end
end
thing()
end
some()
hello()
To explain what @Auxintic posted, locals are basically âloadedâ into your local memory which makes accessing them a little bit quicker than global functions or variables.
This also means like @MillaGamerF said, that you can access global functions and variables anywhere in your script no matter where it was defined. Whereas with local functions and variables you can only access it in the function in which you define it.
Hope this helps, let me know if you need more clarification!