Filesystem

loadfile

Loads a workspace Luau file as a function without executing it immediately.

Syntax

loadfile(path: string, chunkName?: string): function | string | nil
Aliases loadfileasyncdofiledofileasync

Arguments

NameTypeDescription
pathstringWorkspace-relative path to the file or folder.
chunkName?stringOptional name used in debug output for the loaded chunk.

Returns

NameTypeDescription
chunkfunctionLoaded function when compilation succeeds.
errorMessagestring | nilError 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