loadfile
Reads and compiles a workspace Luau file without executing it. A successful call returns a function. Compilation failures return nil and an error message. File-access failures can raise errors. The compiled chunk retains its workspace-relative filename in debug information and error locations.
Syntax
loadfile(path: string) -> (function?, string?)
Also available as
load_file
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | Workspace-relative source file. |
Returns
| Type | Description |
|---|---|
function? | Compiled closure on success. |
string? | Compilation error on failure. |
Description
File operations are confined to the current workspace. Absolute paths and traversal outside it are not accepted. Use a file dialog when the user needs to choose an external file.
Example
writefile("example.luau", "return 2 + 3")
local fn, problem = loadfile("example.luau")
assert(fn, problem)
print(fn())
These are Game executor contracts. Check build requirements and session lifetimes before using them.
