Lua · Examples

Storage and web

Keep data between sessions, write files, fetch from adolfjon.uz.

Each snippet runs as-is: copy it, press From Clipboard on the Lua page, turn it on. Quick start

#Best score with db

db survives restarts. nil deletes.

storage and web_1.lua
local best = db.get("best") or 0
local round = 0

function on_kill(e)
    round = e.round_kills
    if round > best then
        best = round
        db.set("best", best)
    end
end

#CSV log in a file

Only the script's own data folder.

storage and web_2.lua
local rows = { "time,victim,weapon,hs" }

function on_kill(e)
    rows[#rows + 1] = string.format("%.0f,%s,%s,%s", globals.time(), e.victim, e.weapon, tostring(e.headshot))
    files.write("kills.csv", table.concat(rows, "\n"))
end

#JSON from adolfjon.uz

Needs -- permissions: network. Replies come back later.

storage and web_3.lua
-- permissions: network
http.get("https://adolfjon.uz/manifest.json", function(ok, body)
    if not ok then return print("offline") end
    local data = json.decode(body)
    if data then print("site name:", data.name) end
end)

#Copy to clipboard

One click to share.

storage and web_4.lua
function on_menu()
    if menu.button("Copy my stats") then
        local me = game.local_player()
        clipboard.set(json.encode({ map = globals.map(), hp = me and me.health or 0 }))
    end
end

Full scripts

Complete scripts with a header and settings, ready to use or change.