Modules and Packages

Modules help you organize Lua code across multiple files. You typically use require to load them.

Creating a Module:

-- file: mathutils.lua
local mathutils = {}

function mathutils.add(a, b)
  return a + b
end

return mathutils

Using the Module:

local mathutils = require("mathutils")
print(mathutils.add(5, 3)) -- Output: 8

Lua searches for the file using package.path. Make sure your file is in a path Lua can find.

← Prev