Syntax
loadfile(path: string, chunkName?: string): function | string | nilAliases
loadfileasyncdofiledofileasyncArguments
| Name | Type | Description |
|---|---|---|
path | string | Workspace-relative path to the file or folder. |
chunkName? | string | Optional name used in debug output for the loaded chunk. |
Returns
| Name | Type | Description |
|---|---|---|
chunk | function | Loaded function when compilation succeeds. |
errorMessage | string | nil | Error message returned when loading or parsing fails, otherwise nil. |
Description
Loads a workspace Luau file as a function without executing it immediately.
Call it with 2 parameter(s): path, chunkName. The argument table explains which values are required and which ones only refine the behavior.
It returns chunk (function), errorMessage (string | nil). Use the returns table to separate successful values from nil results and recoverable errors.
Example
Compile a local Luau file and handle compilation errors.
local chunk, message = loadfile("workspace/examples/module.luau", "ExampleModule")
if chunk then
chunk()
else
warn(message)
end