Loading and saving data
The Disk API allows you to persistently store and retrieve data for players or general use. Useful for tracking stats, progress, or saving any kind of JSON-compatible values between server restarts.
Overview
DiskApi.loadFile(fileName, async, global)
Loads the contents of a file into memory. Must be called before accessing any stored values with getVar
DiskApi.saveFile(fileName, async, global)
Saves the in-memory data for the specified file to disk. Call this to persist changes made via setVar
. This will also clear the data from the memory (ram)
DiskApi.getVar(fileName, key, fallbackValue, global)
Retrieves the value associated with key
from the in-memory file. If the key doesn't exist, returns fallbackValue
DiskApi.setVar(fileName, key, value, global)
Sets the value of key
to value
in the in-memory file. This does not save to disk until saveFile
is called
When storing and loading player data, make sure to separate the files for each player. Also, always save the player's file when they leave to prevent memory leaks from accumulating as new players join!
Parameters
fileName
String
Name of the file (no extension). Should be unique per player or purpose.
key
String
The variable name to store or retrieve.
value
Any
Value to be saved. Will be serialized using JSON.stringify
.
fallbackValue
Any
Value to return if the key does not exist.
async
Boolean
If true
, runs the load/save operation in a separate thread.
global
Boolean
If true
, the file is shared across all scripts; otherwise script-specific.
Async Explained
Setting async = true
will cause the file operation to run in a separate thread, preventing lag or stalling the main server thread.
However, since file I/O will be delayed, you should wait before accessing or modifying data by using:
Using registerSchedule
with the second parameter set to 0
ensures the code runs in a separated thread.
Global vs Local Storage
global = false
: Data is saved per script, useful for isolated storage.global = true
: Data is shared across scripts, use for shared access.
Last updated