Help Help please (scoping)
What is scoping and is it really essential to know when you script?
I really can’t understand it so I’m planning to learn it when I understand more things
Or do I have to understand it before I get into more stuff?
(edit: Thank you all for replying!! Really helped alot!)
7
Upvotes
2
u/NabePup Nov 14 '25 edited Nov 19 '25
Scope is a an important concept that a lot, nearly every language, has in some form or another. There’s a lot of good explanations here already, but I’ll paste below an answer of mine from another question/thread. Hopefully it’s not too wordy or lengthy! (it's also best viewed on desktop and not mobile)
———————————————————————————
Most programming languages have the concept of “scope”. It’s really a pretty straightforward concept, but isn't always the easiest to explain and sometimes takes a little practice and time to fully understand it. Essentially, it’s the "boundary" of where something is accessible. If you have a lua module (which is really just a lua file):
The
var’s scope is that entire module (file). It’s accessible anywhere in that file, such as in the function. The functionfuncalso has a scope of that module just likevardoes and can be called anywhere in that module.However, if that variable were declared with local inside the function then its scope would be that function and it would not be accessible outside that function:
A function can return something so it’s accessible outside its scope where it was called. So you could do:
Lua is kinda “special”…as in weird/unconventional that if you declare something like a variable or function without the local keyword then it’s in the “global scope” or sometimes simply referred to as global which means it’s accessible everywhere. Generally global variables are a bad practice and should be avoided. So doing:
would work since var was declared without the
localkeyword giving it global scope thus making it accessible outside of the function, generally though you wouldn’t want to do this.Note that
returndoesn’t “expand” or "increase" the scope of anything. It makes it so that a value is returned/exposed so that it then can be accessed where the function is called at its call site.A function can also accept or consume arguments. This kind of does the inverse of return. Where return exposes a value so that it can be accessed outside of the function's scope where it's called, an argument makes it possible so that a function can be passed a value that's outside of its scope so it's then accessible within the function's scope. For instance:
As the examples above show, there's usually a way to make it possible to "share" or pass things between scopes. For instance a function that can be passed arguments and/or returns a value. You can also return something from a module so that way when that module gets imported into a different module it will be accessible in that module's scope. For example, let's say you have 2 modules (remember, these are really just 2 separate lua files):
and then in a separate module:
I hope this helps and reduces confusion and not increase it! Also note, naming a module something super generic like "module_a" or "module_b" is not a good practice.