CS2 Lua API

database

The global database table is a "key → value" store that survives a restart of the script, the cheat and the game. Reading and writing work like a regular table: database.key, database[key] = value.

Rules#

  • The key is a string only. database[1] — error string expected.
  • The store is shared by all scripts: the same key in two scripts is the same value.
  • Changes are saved a few seconds after a write and when the cheat is unloaded.
  • A missing key reads as nil.

Prefix keys with the name of your script ("my_script.counter") so you do not overwrite another script's data.

database[key]#

Reads a value.

Parameter Type Description
key string key

Returns: the saved value or nil.

A table you read is a new copy. Changes to it are not saved until you assign it back: database[key] = t.

local KEY = "my_script.stats"

local stats = database[KEY] or { runs = 0 }
stats.runs = stats.runs + 1
database[KEY] = stats            -- without this line the change is lost

print("The script has started ", stats.runs, " times")

database[key] = value#

Writes a value.

Parameter Type Description
key string key
value any value (see the table)
Value What is saved
nil nil (the key reads as nil)
boolean, string as is
integer integer
fractional number fractional number
a table with keys 1..n without holes array
another table dictionary
function, userdata, cdata (vector, color, 64-bit numbers) nil
a table deeper than 16 levels nil

Metatables are not saved.

Returns: nothing.

-- Save a color as a table of numbers: a color itself cannot be saved
local col = color(255, 128, 0, 255)
database["my_script.color"] = { col.r, col.g, col.b, col.a }

local t = database["my_script.color"]
local restored = color(t[1], t[2], t[3], t[4])
print(restored)   --> Color(255, 128, 0, 255)