What is the difference between
local function A()
end
and
function B()
end
?
What is the difference between
local function A()
end
and
function B()
end
?
The difference is the scope, that is where it can be used.
local means it can only be used within the block of code where it is defined. Without the local specifier the function is global, which means it could be called from anywhere. Generally unless you are gunuinely creating a utility function that can be used anywhere in your game then you should use local, even then it is probably better to put the function in a module.
function iAmAvailableToTheWorld()
end
local function iAmOnlyAvailableInThisScript()
end
Local only applies when in some sort of loop. It basically says that variables or functions declared in loops will stay in the loop. Ie you cannot use the function or variable outside the loop. It is usually good practise to always use local, except in some cases such as modules