When do i use local and non-local functions?

True, but I would call that probelm a separate one from name collision. Using local does prevent name collision, but doesn’t prevent this problem.

difference is speed local functions or variables tend to be more fast than gloabls ones

The difference is negligible, not fast.

That is, what I said in my first post.
We were talking about forward declaration, in the post that you replied to, not local vs global variables.

So are there any solutions to the question I asked?

Just use local variables all the time, you will only use global variables/function when you need to share a variable/function in the entire script. Using local variables/functions prevents name collision, and is, though insignificantly, faster than using global variables.

So every function in every script should be local

Unless you have an explicit reason to not do so, that is correct. And if you don’t know what those reasons might be, you do not have one.

I heard something about it being best to use the this method
local function func()

end)

something about in not being exploitable. I’m not exactly sure and could be making this all up, just something I heard and stuck to. But there isn’t a difference when it comes to normal use.

Yes but I like making variables for functions like local Function = function()

And that’s alright, not saying you should switch. Just saying why I use what I use.

What? There is literally nothing exploitable using global functions and yes, there is a difference when using global and local functions.

I’m asking when do I use them like when do I use a local function over a non-local one?

Always use local functions over globals, only should you use globals when you want to call the function anywhere in the script .

A local function defined as such…

local function ABC() end

…is literally just syntactic sugar for:

local ABC
ABC = function() end

I would prefer using local function ABC() ... end in most cases unless you’re forced to assign a function to the variable at a different time.


In terms of when to use them: always. Always prefer local variables over global. They’re faster for Lua to access due to the different memory locations used for each (albeit it’s a small difference, but good practice nonetheless).

6 Likes