loadfile
Compiles and loads Luau source from a file.
function loadfile(path: string, chunkname: string?): (function?, string?)Synopsis
How it works
Reads a file from the workspace and compiles its contents as a Luau chunk, returning the compiled function without executing it. Equivalent to
loadstring(readfile(path), chunkname). Returns nil, errorMessage if the file has syntax errors. The optional chunkname sets the debug name shown in stack traces.Lazy loading
Useful for deferring execution — you can compile multiple scripts up front and call them later. The returned function is a standard Luau closureClosureA function value that captures its lexical environment. In Luau, closures come in two types: LClosure (Luau bytecode + Proto + upvalues) and CClosure (native C function pointer + upvalues). that can be stored, passed, or hooked like any other.
Usage
Load and execute a module
local fn, err = loadfile("modules/util.lua")
assert(fn, err)()
Parameters
path string Path to the .lua / .luau file.
chunkname string optionalOptional chunk name for error messages.
Returns
function? The compiled chunk, or nil on error.
string? The error message if compilation failed.