> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jaksam-scripts.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Garages and dealerships

> Make garages and vehicle shops give the right keys: what works by itself, and the line to add to esx_vehicleshop, qb-vehicleshop, qb-garages, qbx_vehicleshop and qbx_garages

## How owned cars get their keys

The keys of a car a player **owns** come from the database: `owned_vehicles` on ESX, `player_vehicles` on QBCore and Qbox. Vehicles Keys reads that table when it starts, and then **watches it**: when a shop saves a new car for a player, the player gets its keys **in about 1 to 3 seconds**, with no line to add.

So most garages and shops work without changes:

* **Taking a car out of a garage**: the car is already in the database, its keys already exist. Nothing to do
* **Buying a car**: the shop saves the car, the keys arrive by themselves

> [!NOTE]
>
> The watch is on when `CONTINUOUSLY_REFRESH_PLAYERS_OWNED_VEHICLES = true` in `vehicles_keys/integrations/sv_integrations.lua` (the default). If you turned it off, add the lines of this page to every shop.

## Already integrated

These scripts give the keys by themselves, nothing to do:

| Script | What it does |
| - | - |
| **Jobs Creator** | Its garages give the keys of every vehicle they spawn |
| **Dealerships Creator** | Gives the keys of the cars bought, and turns hotwiring off inside the showroom |
| **Doors Creator** | Shares the lockpick: the closest door or car is picked |

## When you need a line

| Your script... | What goes wrong | The fix |
| - | - | - |
| Sends the old `vehiclekeys:client:SetOwner` event | Vehicles Keys understands it, but it gives a **temporary** key. On a car the player owns, the key turns into **Temporary** until the next restart: no copies to give, no Change lock | Replace it, see below |
| Calls the export of another keys script (for example `qbx_vehiclekeys`) | An error in the console, no keys | Replace it with a Vehicles Keys line |
| Changes the owner of a car that already exists (a sale between players) | The watch sees only cars that are added or removed: the buyer has no keys, the seller keeps them | Refresh both players |
| Gives a car that is not saved (test drive, rental) | No keys | Give a temporary key |

### The lines

```lua Server side theme={null}
-- After a purchase or a change of owner: reloads the owned cars of that player from the database
exports["vehicles_keys"]:refreshPlayerOwnedVehicles(playerId)

-- A car that is not saved (test drive, rental): temporary key, gone at the next restart
exports["vehicles_keys"]:giveVehicleKeysToPlayerId(playerId, plate, "temporary")
```

```lua Client side theme={null}
-- In place of TriggerEvent('vehiclekeys:client:SetOwner', plate): a temporary key only if the player has no key yet
if not exports["vehicles_keys"]:doesPlayerOwnPlate(plate) then
    TriggerServerEvent("vehicles_keys:selfGiveVehicleKeys", plate)
end
```

Every key given by these lines is **virtual**, also with [keys as items](https://docs.jaksam-scripts.com/vehicles-keys/guides/keys-as-items.md) on.

## esx\_vehicleshop

**Nothing to do.** The current `esx_vehicleshop` (ESX Legacy Addons) saves every car in `owned_vehicles`, so the keys arrive by themselves, both when a player buys a car and when a car dealer sells one.

Only if you turned the automatic watch off, open `esx_vehicleshop/server/main.lua`:

**1. A player buys a car**

In the `esx_vehicleshop:buyVehicle` callback, find:

```lua theme={null}
xPlayer.showNotification(TranslateCap('vehicle_belongs', plate))
```

and add this line below it:

```lua theme={null}
exports["vehicles_keys"]:refreshPlayerOwnedVehicles(source)
```

**2. A car dealer sells a car**

In the `esx_vehicleshop:setVehicleOwnedPlayerId` event, find:

```lua theme={null}
xTarget.showNotification(TranslateCap('vehicle_belongs', plate))
```

and add this line below it:

```lua theme={null}
exports["vehicles_keys"]:refreshPlayerOwnedVehicles(xTarget.source)
```

## qb-vehicleshop

Buying a car works by itself. The old key event and the sales between players need a change.

**1. Purchases and test drives (client.lua)**

In `qb-vehicleshop/client.lua` there are **3** lines like this one (two test drives and the showroom purchase):

```lua theme={null}
TriggerEvent('vehiclekeys:client:SetOwner', vehPlate)
```

Replace **each** of them with:

```lua theme={null}
if not exports["vehicles_keys"]:doesPlayerOwnPlate(vehPlate) then
    TriggerServerEvent("vehicles_keys:selfGiveVehicleKeys", vehPlate)
end
```

**2. Sales between players (server.lua)**

In `qb-vehicleshop/server.lua`, in the `transfervehicle` command, there are **3** lines like this one:

```lua theme={null}
TriggerClientEvent('vehiclekeys:client:SetOwner', buyerId, plate)
```

Replace **each** of them with:

```lua theme={null}
exports["vehicles_keys"]:refreshPlayerOwnedVehicles(buyerId)
exports["vehicles_keys"]:refreshPlayerOwnedVehicles(src)
```

The buyer gets the keys, the seller loses them.

## qb-garages

Open `qb-garages/client.lua` and, in the `qb-garages:client:takeOutGarage` event, find:

```lua theme={null}
TriggerEvent('vehiclekeys:client:SetOwner', vehPlate)
```

Replace it with:

```lua theme={null}
if not exports["vehicles_keys"]:doesPlayerOwnPlate(vehPlate) then
    TriggerServerEvent("vehicles_keys:selfGiveVehicleKeys", vehPlate)
end
```

Your own cars keep their **Owned** key. A car of somebody else taken from a shared garage gets a temporary key.

## qbx\_vehicleshop

`qbx_vehicleshop` gives the keys through `qbx_vehiclekeys`, which you removed when you installed Vehicles Keys.

**1. The keys function (config/server.lua)**

Open `qbx_vehicleshop/config/server.lua` and find:

```lua theme={null}
giveKeys = function(src, plate, vehicle)
    exports.qbx_vehiclekeys:GiveKeys(src, vehicle)
end,
```

Replace it with:

```lua theme={null}
giveKeys = function(src, plate, vehicle)
    if not exports.vehicles_keys:isPlayerOwnerOfVehiclePlate(src, plate) then
        exports.vehicles_keys:giveVehicleKeysToPlayerId(src, plate, "temporary")
    end
    exports.vehicles_keys:refreshPlayerOwnedVehicles(src)
end,
```

Test drives get a temporary key. Bought cars get the temporary key at once, and the **Owned** key two seconds later.

**2. Sales between players (server/main.lua)**

In the `transfervehicle` command of `qbx_vehicleshop/server/main.lua`, find:

```lua theme={null}
config.giveKeys(buyerId, row.plate, vehicle)
```

and add this line below it, so the seller loses the keys:

```lua theme={null}
exports.vehicles_keys:refreshPlayerOwnedVehicles(source)
```

## qbx\_garages

Open `qbx_garages/server/spawn-vehicle.lua` and find:

```lua theme={null}
TriggerClientEvent('vehiclekeys:client:SetOwner', source, playerVehicle.props.plate)
```

Replace it with:

```lua theme={null}
if not exports.vehicles_keys:isPlayerOwnerOfVehiclePlate(source, playerVehicle.props.plate) then
    exports.vehicles_keys:giveVehicleKeysToPlayerId(source, playerVehicle.props.plate, "temporary")
end
```

The lines above it that lock the doors need no change.

## Any other script

For a paid or private script we can't read, the idea is always the same:

**1. Find where the car is given**

Search the server files for `INSERT INTO owned_vehicles` or `INSERT INTO player_vehicles` (a purchase), `UPDATE ... SET owner` or `SET citizenid` (a change of owner), or the spawn of the car (a garage).

**2. Look for another keys script**

Search for `vehiclekeys`, `GiveKeys`, `SetOwner` or the name of your old keys script. Those lines must go: replace them with the lines in [The lines](#the-lines).

**3. Add the line**

After a purchase or a change of owner, `refreshPlayerOwnedVehicles` for every player involved. For a car that is not saved, `giveVehicleKeysToPlayerId` with `"temporary"`.

More examples with screenshots for other shops are in [Fix hotwiring bought car](https://docs.jaksam-scripts.com/vehicles-keys/fix-hotwiring-bought-car.md). For the `/car` command, see [How to give keys with /car command](https://docs.jaksam-scripts.com/vehicles-keys/how-to-give-keys-with-car-command.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.