Filesystem

Read and write files in the Lynx workspace.

10 functionssUNC 100%Lynx 0.0.0

Every path is relative to the workspace folder. Scripts can't reach anything outside it, and can't write file types Windows would execute.

Files

readfile

since 1.0.0
readfile(path: string): string

Returns the contents of the file at path. Errors if the file doesn't exist.

Parameters

pathstring
Path relative to the workspace.

Returns

string

The file's contents. Binary-safe.

Example

writefile("hello.txt", "Hello from Lynx")
print(readfile("hello.txt")) --> Hello from Lynx

writefile

since 1.0.0
writefile(path: string, data: string): ()

Writes data to path, replacing the file if it exists. Strings are binary-safe, so this works for images and other binary data too. Missing parent folders are created.

Parameters

pathstring
Path relative to the workspace.
datastring
The new contents.

Example

local HttpService = game:GetService("HttpService")
writefile("session.json", HttpService:JSONEncode({
placeId = game.PlaceId,
savedAt = os.time(),
}))

appendfile

since 1.0.0
appendfile(path: string, data: string): ()

Appends data to the end of the file at path, creating the file if it doesn't exist.

Parameters

pathstring
Path relative to the workspace.
datastring
The data to append.

Example

local function log(...)
local line = os.date("[%H:%M:%S] ") .. table.concat({ ... }, " ")
appendfile("session.log", line .. "\n")
end
log("joined", game.PlaceId)

loadfile

since 1.0.0
loadfile(path: string): (function?, string?)

Compiles the file at path and returns it as a function without running it. On a syntax error, returns nil and the error message. Errors if the file doesn't exist.

Parameters

pathstring
Path relative to the workspace.

Returns

(function?, string?)

The compiled chunk, or nil and an error.

Example

local chunk, err = loadfile("modules/utils.luau")
if not chunk then
error(err)
end
local utils = chunk()
utils.greet()

isfile

since 1.0.0
isfile(path: string): boolean

Returns whether a file exists at path. Returns false for folders.

Parameters

pathstring
Path relative to the workspace.

Returns

boolean

true if the file exists.

Example

if not isfile("config.json") then
writefile("config.json", "{}")
end

delfile

since 1.0.0
delfile(path: string): ()

Deletes the file at path. The file is removed permanently and doesn't go to the Recycle Bin.

Parameters

pathstring
Path relative to the workspace.

Example

if isfile("session.log") then
delfile("session.log")
end

Folders

listfiles

since 1.0.0
listfiles(path: string): {string}

Returns the paths of every file and folder directly inside path. Returned paths are relative to the workspace, so you can pass them straight to other file functions. Pass "" for the workspace root.

Parameters

pathstring
Folder path relative to the workspace.

Returns

{string}

Child paths.

Example

for _, path in listfiles("") do
local kind = if isfolder(path) then "folder" else "file"
print(kind, path)
end

isfolder

since 1.0.0
isfolder(path: string): boolean

Returns whether a folder exists at path.

Parameters

pathstring
Path relative to the workspace.

Returns

boolean

true if the folder exists.

Example

print(isfolder("")) --> true, the workspace root

makefolder

since 1.0.0
makefolder(path: string): ()

Creates a folder at path, including any missing parent folders. Does nothing if it already exists.

Parameters

pathstring
Path relative to the workspace.

Example

makefolder("my-tool/cache/images")
print(isfolder("my-tool/cache")) --> true

delfolder

since 1.0.0
delfolder(path: string): ()

Deletes the folder at path and everything inside it, permanently.

Parameters

pathstring
Path relative to the workspace.

Example

if isfolder("my-tool/cache") then
delfolder("my-tool/cache")
end