dofile
Loads and executes a Luau file.
Syntax
dofile(path: string) -> ()Aliases
runfile
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | Path to the Luau file |
Returns
This function does not return values from the executed file.
Description
dofile reads a Luau file from the workspace folder, compiles it, and executes it. Compilation and runtime errors propagate to the caller. Values returned by the file are discarded.
Example
-- Create a script file
writefile("greet.lua", [[
print("Hello from greet.lua!")
]])
-- Compile and execute it
dofile("greet.lua")Execute Script
-- Create and run a script
writefile("script.lua", [[
print("Script executed!")
for i = 1, 5 do
print("Count:", i)
end
]])
dofile("script.lua")Notes
- This is a yielding function
- File must contain valid Luau code
- Return values from the file are discarded; use
loadfilewhen they are needed - Errors in the file will propagate