# Vehicle

Server-side vehicle utilities for plate generation and garage saving.

***

## GeneratePlate

Generates a unique vehicle license plate that does not already exist in the database.

```lua
local plate = KOJA.Server.GeneratePlate()
```

Also available as an export:

```lua
local plate = exports['koja-lib']:GeneratePlate()
```

**Returns** `string` — unique plate string (e.g. `"ABCD1234"`)

The format is controlled by `Config.SaveVehicleConfig`:

```lua
Config.SaveVehicleConfig = {
    Charset       = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
    NumberCharset = "0123456789",
    Letters       = 4,
    Numbers       = 4,
    Separator     = "",
}
```

> The function queries the database to ensure uniqueness. It loops until it finds an unused plate, so it may take more than one tick for busy servers.

***

## GetRandomString

Generate a random string from a given character set.

```lua
local str = KOJA.Server.GetRandomString(length, charset)
```

| Parameter | Type     | Description                 |
| --------- | -------- | --------------------------- |
| `length`  | `number` | Length of the output string |
| `charset` | `string` | Characters to pick from     |

**Returns** `string`

**Example**

```lua
local code = KOJA.Server.GetRandomString(6, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
-- e.g. "K3X9TL"
```

***

## SaveVehicleToGarage

Save a vehicle to the player's garage in the database. Supports ESX (`owned_vehicles`) and QBCore (`player_vehicles`) table schemas.

```lua
KOJA.Server.SaveVehicleToGarage(data)
```

### data fields

| Field           | Type     | Description                                   |
| --------------- | -------- | --------------------------------------------- |
| `player`        | `table`  | Framework player object (for webhook logging) |
| `identifier`    | `string` | Player character identifier                   |
| `vehicle.name`  | `string` | Vehicle model name (e.g. `"adder"`)           |
| `vehicle.plate` | `string` | License plate (use `GeneratePlate()`)         |
| `vehicle.price` | `number` | Purchase price (logged only)                  |

**Example**

```lua
local plate = KOJA.Server.GeneratePlate()
local player = KOJA.Server.GetPlayerBySource(source)
local identifier = KOJA.Server.GetPlayerIdentifier(source)

KOJA.Server.SaveVehicleToGarage({
    player     = player,
    identifier = identifier,
    vehicle    = {
        name  = 'adder',
        plate = plate,
        price = 500000,
    },
})
```

> `SaveVehicleToGarage` also sends a Discord webhook log if a URL is configured for `'main'` in `KOJA.Server.Webhooks`.

## 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.
- [Inventory](/koja-lib/api-reference/server-api/inventory) — Server-side inventory functions.
- [Notifications](/koja-lib/api-reference/server-api/notifications) — Send notifications to a player from the server side.
- [Server API](/koja-lib/api-reference/server-api) — back to the section overview
