Require don't work (SOLVED)

When I try to access functions inside other script, don’t work.

-- I dont now why dont work
local my_script = require"my_script" 

function init(self)
my_script.fuction_1_inside_my_script()
end

But when I just write like this works.

-- this works
require "my_script" 

function init(self)
fuction_1_inside_my_script()
end

I try different ways, like adding the scripts in one single folder and write: require “scripts.my_script” but don’t work. If I add the line "local my_script = "…

I’m assuming your module looks something like this:

function function_1_inside_my_script()
    -- Some code
end

You need to create a table in your module to put all the functions into:

-- You could name this variable anything, but 'M' is the standard
local M = {}

function M.function_1_inside_my_script()
    -- Some code
end

return M

Then the first method in your post will work. require will return whatever value the module itself returns, in this case M. See the manual page about modules for more details.

6 Likes