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
esx_vehicleshop
Nothing to do. The currentesx_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 and add this line below it:
esx_vehicleshop:buyVehicle callback, find:2
A car dealer sells a car
In the and add this line below it:
esx_vehicleshop:setVehicleOwnedPlayerId event, find: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 Replace each of them with:
qb-vehicleshop/client.lua there are 3 lines like this one (two test drives and the showroom purchase):2
Sales between players (server.lua)
In Replace each of them with:The buyer gets the keys, the seller loses them.
qb-vehicleshop/server.lua, in the transfervehicle command, there are 3 lines like this one:qb-garages
Openqb-garages/client.lua and, in the qb-garages:client:takeOutGarage event, find:
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 Replace it with:Test drives get a temporary key. Bought cars get the temporary key at once, and the Owned key two seconds later.
qbx_vehicleshop/config/server.lua and find:2
Sales between players (server/main.lua)
In the and add this line below it, so the seller loses the keys:
transfervehicle command of qbx_vehicleshop/server/main.lua, find:qbx_garages
Openqbx_garages/server/spawn-vehicle.lua and find:
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"./car command, see How to give keys with /car command.