Storage, files, JSON and HTTP
Saving data, files, JSON, web requests and maths helpers.
storage.get(key [, default]) -> anyA stored value (saved in scripts/data/<script>/store.json, 1024 keys / 1 MiB).
| Name | Type | Description |
|---|---|---|
key | string | |
defaultoptional | any |
any
local n = storage.get("kills", 0)storage.set(key, value) -> booleanStores a JSON-compatible value (tables nested at most 63 deep, else an error); false when over the size limit. Written at unload and every 5 s.
| Name | Type | Description |
|---|---|---|
key | string | |
value | any |
boolean
storage.set("kills", n + 1)storage.remove(key) -> booleanRemoves a key.
| Name | Type | Description |
|---|---|---|
key | string |
boolean
storage.remove("kills")storage.keys() -> string[]All stored keys.
string[]
for _, k in ipairs(storage.keys()) do endfiles.read(path) -> string | nil, stringReads a file from scripts/data/<script>/ (paths are relative to it).
| Name | Type | Description |
|---|---|---|
path | string |
string | nil, string
local text = files.read("notes.txt")files.write(path, data) -> boolean | nil, stringWrites a file in the data folder (8 MiB per script). No ':' / device names / trailing dots; store.json is reserved.
| Name | Type | Description |
|---|---|---|
path | string | |
data | string |
boolean | nil, string
files.write("notes.txt", "hi")files.append(path, data) -> boolean | nil, stringAppends to a file in the data folder.
| Name | Type | Description |
|---|---|---|
path | string | |
data | string |
boolean | nil, string
files.append("log.txt", line .. "\n")files.exists(path) -> booleanWhether a file exists.
| Name | Type | Description |
|---|---|---|
path | string |
boolean
if files.exists("notes.txt") then endfiles.list([dir]) -> string[] | nil, stringEntries of a folder in the data folder (folders end with /).
| Name | Type | Description |
|---|---|---|
diroptional | string |
string[] | nil, string
for _, f in ipairs(files.list()) do endfiles.remove(path) -> boolean | nil, stringDeletes a file in the data folder.
| Name | Type | Description |
|---|---|---|
path | string |
boolean | nil, string
files.remove("notes.txt")json.encode(value [, pretty]) -> stringEncodes tables, strings, numbers, booleans (depth <= 64). Empty tables encode as [].
| Name | Type | Default | Description |
|---|---|---|---|
value | any | ||
prettyoptional | boolean | false |
string
local s = json.encode({a = 1})json.decode(text) -> any | nil, stringDecodes JSON (at most 1 MiB, depth <= 64); null becomes json.null.
| Name | Type | Description |
|---|---|---|
text | string |
any | nil, string
local t = json.decode('{"a": 1}')The JSON null value.
lightuserdata
Everything here needs Allow internet connections in Scripts > Security (off by default).
http.get(url [, opts, callback]) -> integer | nil, stringAsynchronous GET; opts = {headers = {...}}. callback(res) runs on a later frame with res = {status, body, headers, error}. http/https only, public addresses only (no localhost / LAN / link-local), 5 redirects, 10 s timeout, 2 MiB.
| Name | Type | Description |
|---|---|---|
url | string | |
optsoptional | table | |
callback | function |
integer | nil, string
http.get("https://example.com/api", function(res) log.info(res.status) end)http.post(url, body [, opts, callback]) -> integer | nil, stringAsynchronous POST.
| Name | Type | Description |
|---|---|---|
url | string | |
body | string | |
optsoptional | table | |
callback | function |
integer | nil, string
http.post(url, json.encode({a = 1}), {headers = {["Content-Type"] = "application/json"}}, function(res) end)http.request(spec, callback) -> integer | nil, stringspec = {url=, method="GET"|"POST"|"PUT"|"PATCH"|"DELETE"|"HEAD", body=, headers=}.
| Name | Type | Description |
|---|---|---|
spec | table | |
callback | function |
integer | nil, string
http.request({url = url, method = "PUT", body = "x"}, function(res) end)math.clamp(v, lo, hi) -> numberClamps v to [lo, hi].
| Name | Type | Description |
|---|---|---|
v | number | |
lo | number | |
hi | number |
number
math.clamp(x, 0, 1)math.lerp(a, b, t) -> numberLinear blend.
| Name | Type | Description |
|---|---|---|
a | number | |
b | number | |
t | number |
number
math.lerp(0, 100, 0.5)math.round(v [, step]) -> numberRounds to a multiple of step.
| Name | Type | Default | Description |
|---|---|---|---|
v | number | ||
stepoptional | number | 1 |
number
math.round(3.14159, 0.01)