Programming in Lua : 6.2 This means that a variable declared outside the block can be accessed from inside the block. how to define a global function lua Code Example Once the package has an exclusive environment, not only all its functions share this table, but also all its global variables go to this table. However foo is a global variable. Lua Why does Lua default to global variables? - Stack Overflow Lua 5.1 Reference Manual Each function has its own reference to an environment, so that all global variables in this function will refer to this environment table. When a function is created, it inherits the environment from the function that created it. To get the environment table of a Lua function, you call getfenv. To replace it, you call setfenv. How Variables work in Lua with Examples? - EDUCBA The global variables will hold their value throughout the lifetime of your program.