Remember that there is no concept of a "local function".
All functions are anonymous in Lua, which means they have no names. Remember that this code:
function name()
end
Is really just syntactic sugar / sugar syntax for this code:
name = function()
end
The syntax for a "local function" is similar. This code:
local function name()
end
Is sugar syntax for this code:
local name
name = function()
end
You may be wondering why it isn't sugar syntax for this code instead:
local name = function()
end
That would be because in the above example, we wouldn't be able to make recursive function calls.
As for local variables, they are only slightly faster to access. It's almost entirely irrelevant in most situations. Remember, micro-optimization is a waste of time. However, prefixing a variable declaration with local in all scopes allows you to easily see the difference between defining a variable for the first time (or shadowing it) and redefining it.
Consider:
x = 10
We're not sure if we're redefining the x variable as easily. In this example, we can more easily tell that we're defining it for the first time:
local x = 10
For this reason, I recommend prefixing all variable declarations with local.
Additionally, RBXScriptSignal:connect() with a lowercase c is deprecated, switch to RBXScriptSignal:Connect().
2 comments
Local functions are indeed would be slightly faster. Basically, you need to use Global when you want to reach that function in different places, and use Local when you only need to use that in that place. I'll send you an example.
Locals can be reached globally if they're declared in the global scope. That's why it's sort of redundant to use globals unless an awfully specific situation calls for it.