loadfile Synchronous
loadfileasync
Compile a workspace Luau file without running it yet. Keep the returned function when execution belongs at a later point in your script.
Luau
loadfile(path: string) -> (Function) OR (nil, error: string)Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | Workspace-relative path of the Luau source file. |
Returns
(Function) OR (nil, error: string)A compiled function, or nil plus a compilation error string.
Usage notes
Filesystem failures raise. A syntax error returns nil and a message; calling the compiled function can still raise at runtime.
Example
Luau
local path = "loadfile-example.luau"
writefile(path, [[return "Loaded on demand"]])
local chunk, reason = loadfile(path)
if chunk then print(chunk()) else warn(reason) end
delfile(path)