Sharing code with require¶
require loads a module and returns the value the module returns. A module
is a .luau file in the project's scripts/ folder, or a folder below it,
that returns one value, usually a table. Every .luau suffix below also works
with .lua, though having both Name.luau and Name.lua in the same folder
is an error.
-- Server script scripts/Lava.server.luau, attached to a part.
local Settings = require("./Settings")
self.Color = Settings.LavaColor
The path is relative to the requiring file and leaves out .luau: ./Name
loads Name.luau from the same folder, ../Name from the parent folder, and
./folder/Name from a folder below. The console (F9) and Studio's lua_run
tool require from the root of scripts/. @scripts/Name loads
scripts/Name.luau whichever folder the requiring file is in, and returns
the same table as a relative path to that file. Plugin scripts cannot use
@scripts.
A folder with an init.luau (or init.lua) file is a folder module:
./pkg loads pkg/init.luau. Inside pkg/init.luau, ./Sibling resolves a
sibling of pkg and @self/Child resolves pkg/Child.luau. Having both
pkg.luau and pkg/init.luau, or requiring ./pkg/init directly, is an
error.
A module runs once per server and once per player. The first require of a
file runs the module body; every later require returns the same value, so
scripts that require the same module share its table. A module has no self;
pass the part in as an argument instead. Its body cannot
wait, so start timers and loops from a function that a script calls.
Players download every plain .luau module and can read its source. Name a
module with server secrets Name.server.luau: it stays on the server, and only
server scripts can require("./Name.server"). Keep such a module out of
scripts/autorun/, where it also runs as an
autorun script.
Errors¶
no module '<path>': no file exists at the resolved<path>(for example, a typo or a doubled folder in the relative path). When a.client.luauor.server.luaufile with the same name exists, or when therequirepath includes.luau, the message names the existing file too.cannot require '<path>': server scripts (.server.luau) are not available on clientsorcannot require '<path>': it is attached as a server script and is not available on clients: a client script required a.server.luaufile or a.luaufile attached to an entity as a server script. Move shared code into an unattached.luaufile.ambiguous require: both '<path>.luau' and '<path>.lua' exist: both a.luauand a.luafile match the same module path. Remove or rename one of them.ambiguous require: both '<path>/init.luau' and '<path>/init.lua' exist; remove or rename one: bothinit.luauandinit.luaexist in the folder module. Remove or rename one of them.ambiguous require: both '<path>.luau' and '<path>/init.luau' exist; remove or rename one: a file module and a folder module share the same name. Remove or rename one of them.cannot require '<path>/init.luau' directly; omit '/init' in require(): require the folder path without/init.module '<path>' is empty (0 bytes); a module must return a single value: the file exists at<path>, but it is empty. Return a value, such as a table.error requiring module "<path>": require path must start with a valid prefix: ./, ../, or @: writerequire("./Settings"), notrequire("Settings").error while running module: <message>: the module does not compile, or its body stopped with an error; the message names the file and line.module can not yield: the module body waited, for example withtask.wait.cyclic require of '<path>': modules require each other in a loop. Move the shared part into a third module that both require.module must return a single value: the module ends without areturn, or returns more than one value.