Guide: Saving data

Config files that stick around after a restart, encoding them, and how they get corrupted.

The basics

The filesystem is the only thing that lasts after the session ends. Anything you want to keep (a config, a whitelist, a log) gets saved with writefile and loaded with readfile.

Roblox's HttpService does the JSON for you, so you don't need your own encoder.

A config file

config.lua
local HttpService = game:GetService("HttpService")
local PATH = "config/mytool.json"

local DEFAULTS = {
    enabled = true,
    walkSpeed = 32,
    theme = "ocean",
}

local function load()
    if not isfile(PATH) then
        return table.clone(DEFAULTS)
    end

    local ok, decoded = pcall(HttpService.JSONDecode, HttpService, readfile(PATH))
    if not ok or type(decoded) ~= "table" then
        warn("config was corrupt, falling back to defaults")
        return table.clone(DEFAULTS)
    end

    -- Fill in anything a newer version added.
    for key, value in pairs(DEFAULTS) do
        if decoded[key] == nil then
            decoded[key] = value
        end
    end
    return decoded
end

local function save(config)
    if not isfolder("config") then
        makefolder("config")
    end
    writefile(PATH, HttpService:JSONEncode(config))
end

local config = load()
config.walkSpeed = 48
save(config)

Always pcall the decode

A half written file, someone editing it by hand, or a version change will give JSONDecode something it can't read. Without pcall that error happens every single time you join.

Binary and compression

For anything that isn't text, like a buffer or an image you downloaded, Base64 lets you store it in a text file and LZ4 makes it smaller. Both work on their own and under crypt.

blob.lua
local raw = readfile("assets/blob.bin")

local packed = lz4compress(raw)
writefile("cache/blob.lz4", packed)

-- ... and back
local restored = lz4decompress(readfile("cache/blob.lz4"), #raw)

-- Base64 when it has to survive as text.
writefile("cache/blob.b64", base64encode(raw))
local decoded = base64decode(readfile("cache/blob.b64"))

lz4decompress needs the original size, so save it with the compressed data. A small JSON object with two keys is usually easiest.

Sensitive stuff

If a file has something you don't want sitting on disk in plain text, like a webhook URL or a key, the crypt library can encrypt it, hash it, or give you random bytes for a key.

crypt.lua
local key = crypt.generatekey()
local encrypted = crypt.encrypt("https://discord.com/api/webhooks/...", key)
writefile("config/hook.enc", encrypted)

-- Hashing, when you only need to compare rather than recover.
print(crypt.hash("compare me"))
print(crypt.hmac("message", key))

The key has to go somewhere

Encrypting a file and saving the key right next to it doesn't do much. It stops someone casually reading it, not someone who has your workspace folder.

See also

Filesystem for all the file functions, Crypt for encryption, hashing and random, and Encoding for Base64 and LZ4.