# database

### Functions:

#### database.read

`database.read(key_name: string)`: any

| Argument      | Type   | Description                                                                 |
| ------------- | ------ | --------------------------------------------------------------------------- |
| **key\_name** | string | String used as a name of the key. Make sure to write to the same key\_name. |

Gets a value from the database

#### database.write

`database.write(key_name: string, value: any)`

| Argument      | Type   | Description                                                                                         |
| ------------- | ------ | --------------------------------------------------------------------------------------------------- |
| **key\_name** | string | String used as a name of the key.                                                                   |
| **value**     | any    | Value the key should be set to. This can be anything that can be sanitized (no functions, userdata) |

Writes a value to the database. Avoid calling this often. For example, call read at script load, then call write during the 'shutdown' event

### Examples:

```lua
local data = database.read("example-1") or {}
data.load_count = (data.load_count or 0) + 1

client.log("this is the ", data.load_count, ". time you've loaded this script!")

client.set_event_callback("player_death", function(e)
	if client.userid_to_entindex(e.attacker) == entity.get_local_player() then
		data.kill_count = (data.kill_count or 0) + 1
		client.log("this is your ", data.kill_count, ". kill!")
	end
end)

client.set_event_callback("shutdown", function()
	database.write("example-1", data)
end)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gamesensical.gitbook.io/docs/developers/globals/database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
