Category
Files
Read and write files, load code, and use require.
Scripts can only touch files inside one workspace folder. All paths are relative to it, and anything outside it is refused.
Pages in this category
File functions
Functions that fail raise an error with a message.
readfile(path)
Returns the file's contents as a string.
writefile(path, data)
Writes data to the file, replacing it.
appendfile(path, data)
Adds data to the end of the file.
listfiles(path = "")
Returns an array of the entries in a folder. Each entry includes the folder prefix: listfiles("storage") gives "storage/a.json".
isfile(path), isfolder(path)
Return true if the path is a file or a folder.
makefolder(path)
Creates a folder.
delfile(path), delfolder(path)
Delete a file or folder. Both names do the same thing.
makefolder("logs")
writefile("logs/run.txt", "started\n")
appendfile("logs/run.txt", "still running\n")
print(readfile("logs/run.txt"))
Loading code
loadstring(src, name = "loadstring")
Compiles a string of Lua and returns a function. On a syntax error, returns nil and the error message. The function runs in the same sandbox as your script.
local f, err = loadstring("return 1 + 1")
print(f())
loadfile(path)
Compiles a file. Returns the function, or nil and the error.
dofile(path)
Compiles and runs a file, and returns what it returns. Raises an error if loading fails.
require(name)
Loads modules/name.lua from the workspace once and returns what it returns. The result is cached, so later calls return the same value. A module that returns nothing gives true. Raises an error if the file is missing or does not compile.
-- modules/utils.lua
return { double = function(x) return x * 2 end }
-- main script
local utils = require("utils")
print(utils.double(21))