dofile
Loads and executes a Lua file.
Syntax
dofile(path: string) -> any...Aliases
runfile
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | Path to the Lua file |
Returns
| Type | Description |
|---|---|
any... | Values returned by the executed file |
Description
dofile reads a Lua file from the workspace folder, compiles it, and executes it. Any values returned by the file are returned by dofile.
Example
-- Create a script file
writefile("mymodule.lua", [[
local module = {}
module.greeting = "Hello!"
return module
]])
-- Execute and get the return value
local myModule = dofile("mymodule.lua")
print(myModule.greeting) -- "Hello!"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 be valid Lua/Luau code
- Errors in the file will propagate