Doubt about modules and local keyword (SOLVED)

local functions={}

   local function functions.test()
	local a="eee"
	print(a)
end


return functions

Need to put local in all the functions or variables, or a function/variable inside a table/function, are always local?

or if the table/function is local, al his “child” are local?

local functions={}

function functions.test()
	a="eee"  --or this need to be local?
	print(a)
end


return functions

thanks

Second version is correct.

Note that the variable a in that case is global. You might want to transform it into functions.a or local if you don’t want any access to it from outside the module

Then test function is local because its inside a table with local, but the variable is global because is inside a function without local keyword, but this fuction is local.

Im correct?

You can’t compile this code.
function functions.test() already local as a field inside local table functions.

in stack overflow said me that everything inside a local table is local.

then, in a module, there is no need to use local keyword?

local module={}

function module.a()  -- is local?
      variable="a"    --is local??
end

module.b="b"  -- is local??

function module.c()
   tablet={}  -- is local??
end

need some aclaration

1 Like
local module={}

function module.a()  -- is local? yes, module is local
      variable="a"    --is local?? no
end

module.b="b"  -- is local?? yes, module is local

function module.c()
   tablet={}  -- is local?? -no
end
3 Likes

More information about scopes:
https://www.lua.org/pil/4.2.html
http://lua-users.org/wiki/ScopeTutorial

3 Likes

I understand now, thanks

1 Like