> For the complete documentation index, see [llms.txt](https://docs.kojascripts.eu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kojascripts.eu/koja-lib/api-reference/server-api/inventory.md).

# Inventory

Server-side inventory functions. All functions route through the adapter for the detected inventory resource and fall back to the framework's native item API when no adapter is available.

***

## addInventoryItem

Add an item to a player's inventory.

```lua
local ok = KOJA.Server.addInventoryItem(source, name, count, metadata, slot)
```

| Parameter  | Type      | Description                                                                                  |
| ---------- | --------- | -------------------------------------------------------------------------------------------- |
| `source`   | `number`  | Player server ID                                                                             |
| `name`     | `string`  | Item name                                                                                    |
| `count`    | `number`  | Amount to add                                                                                |
| `metadata` | `table?`  | Item metadata (supported by ox\_inventory, qs-inventory, codem-inventory, jaksam\_inventory) |
| `slot`     | `number?` | Target slot (inventory-specific)                                                             |

**Returns** `boolean` — `true` on success.

***

## removeInventoryItem

Remove an item from a player's inventory.

```lua
local ok = KOJA.Server.removeInventoryItem(source, name, count, metadata, slot)
```

Same parameters as `addInventoryItem`.

**Returns** `boolean` — `true` on success.

***

## getInventoryItemCount

Get the total count of an item in a player's inventory.

```lua
local count = KOJA.Server.getInventoryItemCount(source, name, metadata)
```

| Parameter  | Type     | Description                             |
| ---------- | -------- | --------------------------------------- |
| `source`   | `number` | Player server ID                        |
| `name`     | `string` | Item name                               |
| `metadata` | `table?` | Filter by metadata (ox\_inventory only) |

**Returns** `number`

***

## HasItem

Check if a player owns at least `count` of an item.

```lua
local has = KOJA.Server.HasItem(source, name, count)
```

| Parameter | Type      | Description                 |
| --------- | --------- | --------------------------- |
| `source`  | `number`  | Player server ID            |
| `name`    | `string`  | Item name                   |
| `count`   | `number?` | Minimum amount (default: 1) |

**Returns** `boolean`

***

## GetPlayerInventory

Returns the player's full inventory as a flat map.

```lua
local inventory = KOJA.Server.GetPlayerInventory(source)
```

**Returns** `table` — `{ [itemName] = count }`

***

## Examples

```lua
-- Give 3 bread to a player
KOJA.Server.addInventoryItem(source, 'bread', 3)

-- Take 1 lockpick
local ok = KOJA.Server.removeInventoryItem(source, 'lockpick', 1)
if not ok then
    print('Failed to remove lockpick')
end

-- Check before doing something
if not KOJA.Server.HasItem(source, 'keycard') then
    KOJA.Server.SendNotify({ source = source, type = 'error', desc = 'You need a keycard.' })
    return
end

-- Print all items
local inv = KOJA.Server.GetPlayerInventory(source)
for item, count in pairs(inv) do
    print(item, count)
end
```

***

## Metadata & Slot Support

```lua
-- ox_inventory: add item with metadata
KOJA.Server.addInventoryItem(source, 'phone', 1, { serial = '12345', color = 'black' })

-- qs-inventory / codem-inventory / jaksam_inventory: add to a specific slot
KOJA.Server.addInventoryItem(source, 'weapon_pistol', 1, nil, 5)
```

{% hint style="info" %}
When using `qb-inventory` or a framework fallback, `metadata` and `slot` parameters are ignored.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://docs.kojascripts.eu/koja-lib/api-reference/server-api/inventory.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.
