# 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{type="info"}
When using `qb-inventory` or a framework fallback, `metadata` and `slot` parameters are ignored.
:::

## Related pages

- [Police](/koja-lib/api-reference/server-api/police) — Functions for checking how many police officers are online.
- [Callbacks](/koja-lib/api-reference/server-api/callbacks) — The callback system allows server scripts to handle requests from clients and vice versa.
- [Licenses](/koja-lib/api-reference/server-api/licenses) — Check whether a player holds a specific driving or skill license.
- [Money](/koja-lib/api-reference/server-api/money) — Functions for reading and modifying a player's money on the server.
- [Notifications](/koja-lib/api-reference/server-api/notifications) — Send notifications to a player from the server side.
- [Player](/koja-lib/api-reference/server-api/player) — Functions for reading player information on the server.
- [Server API](/koja-lib/api-reference/server-api) — back to the section overview
