0

I'm trying to create a module through the best practice means rather than package.seeall. Below is the code I've used. I'm running into an error where once I run require("mylibrary"), m seems to be returning a nil value.

I'm getting the following error:

[ERROR] gamemodes/mygamemode/gamemode/init.lua:374: attempt to index local 'mm' (a nil value)
  1. addonsandmodules - gamemodes/mygamemode/gamemode/init.lua:374
  2. unknown - gamemodes/mygamemode/gamemode/init.lua:380

from mylibrary.lua

print("DEBUG: prior to creation of moduel done")
local m = {}
print("creation of module done")


function m.soup()
    return "soup is good"


end


function m.roll(num)
    print("rolling the dice...")
    return math.random(1,num)

end

print("DEBUG: functions defined")

m.public_variable = "hello from my_module"


print(m.public_variable)

print("made it right up to return value")


return m

from init.lua

local mm = {require("mylibrary")}

print(mm)


print(mm.public_variable)

I could use package.seeall and kind of skip this but if anyone can help me figure out where I'm going wrong, would be appreciated.

I tried printing debugging steps to see where the module failed. Unfortunately nothing is printing.

2
  • 1
    This error cannot occur in the code you provided. Commented Oct 18, 2025 at 7:29
  • 1
    The errors and behavior you report don't make sense, but you need to remove the curly braces from the call to require. require("mylibrary") runs the code in mylibrary.lua, so here it returns the table m from mylibrary.lua. But you wrap this return value in another table: you could get to the module with, e.g., mm[1].public_variable, but instead load the module with local mm = require("mylibrary") so that you can use mm.public_variable. Commented Oct 18, 2025 at 8:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.