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.

Syntax
Luau
loadfile(path: string) -> (Function) OR (nil, error: string)

Parameters

Function parameters
ParameterTypeDescription
pathstringWorkspace-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

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)
Kawaii documentation