Skip to main content

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
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:

When you need a line

The lines

Server side
Client side
Every key given by these lines is virtual, also with keys as items 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:
and add this line below it:
2

A car dealer sells a car

In the esx_vehicleshop:setVehicleOwnedPlayerId event, find:
and add this line below it:

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):
Replace each of them with:
2

Sales between players (server.lua)

In qb-vehicleshop/server.lua, in the transfervehicle command, there are 3 lines like this one:
Replace each of them with:
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:
Replace it with:
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:
Replace it with:
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:
and add this line below it, so the seller loses the keys:

qbx_garages

Open qbx_garages/server/spawn-vehicle.lua and find:
Replace it with:
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.
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. For the /car command, see How to give keys with /car command.