Vehicles & Keys
How WHIP's cars work: their states, keys that are items, the ignition, locks, seats, storage and the damage they keep. Read it before you write a script that touches a car; the calls are in the Vehicles API.
Vehicle states
| State | Meaning | Entity exists? |
|---|---|---|
'out' | In the world, position tracked | Yes |
'garaged' | Stored in a garage | No |
'impounded' | Seized (admin), retrievable | No |
Cars that are out stay in the world when their player leaves or drives off, and come back where they stood after a restart.
Keys and the ignition
A key is an item, vehicle_key, with metadata { vehicleId, plate, model }, and a token when the car's locks have been changed. It doesn't stack and weighs 0.1 kg.
- Getting one.
GiveVehicleKey, orCreateOwnedVehiclewith{ giveKey = true }(without it, no key).SpawnTempVehiclewithforSourcegives that player one unlessgiveKey = false;keyInIgnition = trueputs one in the ignition instead. - Starting the engine. The driver drags the key into the car's one-slot ignition in the inventory; only a key whose
metadata.vehicleIdmatches fits, and after a change of locks only one cut since. Mouse wheel up starts the engine, down stops it. - No key in the ignition: the engine stays off and wheel up shows "No key in ignition!".
- A spare.
GiveVehicleKeyrefuses a player who already holds a key of the car, unless you pass{ spare = true }, which cuts another one to hand to a friend. - Changing the locks.
ChangeVehicleLocksmakes every key of an owned car cut so far stop fitting, wherever it is, so a lost or copied key is worthless; keys cut after fit.CountVehicleKeyssays how many keys fit the car now.
HoldVehicleEngine keeps it off and shows the driver your reason until ReleaseVehicleEngine. Otherwise a running engine stays on while its key is in the ignition.Locking
Players lock with L, or Lock / Unlock on a door's prompt; the lock is saved with the car. From the driver's seat the key may be in the ignition or the inventory; on foot it must be in the inventory, within Config.VehicleLockRange (8 m). Players nearby see two flashes for lock, one for unlock.
Street cars
| Kind | Comes from | Saved? | vehicleId |
|---|---|---|---|
| Owned | CreateOwnedVehicle, or a street car claimed with ClaimVehicle | Yes | Database id (1, 2, 3...) |
| Session | NPC cars, registered on first use | No | 1,000,000,000 and up |
A street car is registered when a player takes its driver's seat, lockpicks it, opens its boot or refuels it, or a script calls ResolveVehicle. It then has a vehicleId, boot, glovebox, ignition, lock and fuel; if its engine was running, a key is put in its ignition.
Street cars stay locked until lockpicked, then stay unlocked. The rule's range is Config.NpcVehicleLockRange (10 m); 0 turns it off. A resource's local, non-networked car is left alone: its prompts work on that one game and it has no boot.
Seats
Passengers stay in their seat: no sliding into an empty driver's seat, and GTA's shuffle button (H, D-pad right) does nothing. Config.Seats.NoAutoShuffle = false brings GTA's behaviour back. Players change seat with Seats on the F1 wheel or /seats. Read a seat with GetPlayerSeat; whip_core:vehicle:entered fires again on a seat change.
Seat anyone from a script with the seat engine, never with SetPedIntoVehicle or TaskEnterVehicle of your own. It works the same for a player, one of WHIP's NPCs or any other ped. PutInSeat walks them to the seat's own door or puts them in at once, and settles a seat someone sits in (refuse, take another, wait in another and move over, or pull them out). TakeOutOfSeat gets them out, waiting for a moving vehicle to stop or bailing out. HoldInSeat keeps them in until you release it: their exit is held, nobody can pull them out, and they are put back if they are thrown out. Each job runs on the game that runs the ped, and is done only when the server sees them in that seat.
F at a car whose driver is held (a Director rider, a chauffeur a script holds) gets the player in beside them instead of trying to pull them out. A held driver who is fighting that player doesn't let them in. Any other driver is GTA's to pull out, as before.
Storage
Each car's boot (trunk), glovebox and ignition are stores. Their sizes come from the first that is set: the car's own (given when it was made), its model's, its category's (a GTA vehicle class), then the config (VehicleTrunkSlots and the rest). Set models and categories in /whip → Vehicles → Vehicle Adjustments, where either can also turn off its boot or glovebox. Shrinking or turning off slots is refused while a car still has items in them.
Naming a car: vehicle references#
Every vehicle, stat and data export takes the car first, on both sides, in any of these forms:
vehicleId { id = vehicleId } { entity = veh } { plate = 'ABC123' } { netId = netId }- A bare number is always a
vehicleId. Pass an entity handle as{ entity = veh }: both are integers, so guessing could pick a different car. OnlyGetVehicleId(entity)andResolveVehicle(entity)take a handle. - A snapshot from
GetVehicleis a reference too. - On the server, a reference to no car returns
'Vehicle not found'; a table naming nothing returns'Invalid vehicle reference'. A client returnsnilfor a car not streamed in near it.
Writing a vehicle script
On the client, GetVehicleId(entity) returns a car's id from the cache, or nil for a car WHIP hasn't met; ResolveVehicle(entity) then registers it. Send the id to the server. This tow hook works on any car:
-- client.lua
Whip.register(function()
Whip.core:AddAction({
id = 'hook',
at = { vehiclePart = 'trunk' },
label = 'Hook Vehicle',
canInteract = function(ctx) return exports.whip_core:HasItemFlag('tow_hook') end,
onSelect = function(ctx) -- runs once, in its own thread: may wait
local vehicleId, why = exports.whip_core:GetVehicleId(ctx.entity), nil
if not vehicleId then vehicleId, why = exports.whip_core:ResolveVehicle(ctx.entity) end
if not vehicleId then return ctx.refuse(why) end
TriggerServerEvent('tow:hookVehicle', vehicleId)
end,
})
end)
-- server.lua, inside Whip.register
Whip.core:SecureEvent('tow:hookVehicle', {
rateLimit = 2000,
params = { {'vehicle', 'vehicle', { anyOwner = true, session = true }} }, -- anybody's car, street cars too
near = { at = 'vehicle', range = 8.0 }, -- where the car is now
}, function(source, player, v)
print(('Towing %s (%s)'):format(v.model, v.plate))
end)ResolveVehicle waits for the server. Call it in onSelect, a command or another one-off, never in canInteract, a label, a loop or per-frame code. Use GetVehicleId or GetVehicle({ entity = entity }) there.The other client reads are in the Client vehicles API. On the server, GetVehicle returns one snapshot with the owner, lock, state, netId, stats and storage. Sell a car with TransferVehicle and remove one with DeleteVehicle.
Damage a car keeps#
An owned car keeps its engine and body health, torn-off doors and burst tyres, but not its dents. It's saved when the driver gets out, every VehiclePositionSaveInterval, and when the car is stored or leaves the world. It's put back when the car leaves a garage, or after a restart when a player comes near, and only ever adds damage. Config.VehicleDamage.remember = false turns it off.
A wreck comes back with its engine at Config.VehicleDamage.engineFloor (100): smoking, never on fire. The damage is five stats, read with GetVehicleStat:
| Stat | Value |
|---|---|
engineHealth, bodyHealth | 0 to 1000, GTA's scale; below 400 the engine smokes |
doorsOff | Bit n is door n: 0 front left, 1 front right, 2 rear left, 3 rear right, 4 bonnet, 5 boot |
tyresFlat, tyresRim | Bits over GTA's tyre numbers 0, 1, 2, 3, 4, 5, 45, 47 |
ClearVehicleDamage as you fix the car.Vehicle data#
SetVehicleData attaches anything that should stick to a car and isn't a gauge, a mod or an item: a part's grade, a finance flag. Values are anything JSON-encodable, never clamped or drawn. The calls are in Vehicle data & handling.
exports.whip_core:SetVehicleData(vehicleId, 'turbo', { grade = 'A' }) -- server
local turbo = exports.whip_core:GetVehicleData(vehicleId, 'turbo') -- any client| If it is... | Use |
|---|---|
| a number with a range a player should watch move | a vehicle stat |
| items in slots | a store |
| a GTA mod: colour, extra, wheel | properties |
| anything else that should stick to the car | vehicle data |
- A key belongs to the resource that wrote it. Anyone can read it; only the writer can change or clear it.
- It follows the car, not the entity, through garaging, despawning and restarts, and is deleted with the car.
- It isn't a database: 32 keys per resource per car, not indexed. To query it or keep history, build your own table.
- Every player near the car can read it, so keep no secrets in it.
Events#
Server events, for AddEventHandler; a client can't send them. Don't fire them yourself: listeners would hear something that didn't happen.
| Event | Payload | Fires when |
|---|---|---|
whip_core:vehicle:created | { vehicleId, ownerCharId } | Any path makes a vehicle |
whip_core:vehicle:damageCleared | { vehicleId } | After WHIP clears its recorded damage for a repair. A resource can clear its own damage data here; this does not itself repair the game entity |
whip_core:vehicle:ownerChanged | { vehicleId, from, to } | A vehicle changes character |
whip_core:vehicle:deleted | { vehicleId, ownerCharId, wasOut } | A vehicle stops existing. Remove your own per-car data here |
whip_core:vehicle:dataChanged | { vehicleId, key, value, old, cleared, resource } | Vehicle data is set or removed, by resource |
whip_core:vehicle:lockChanged | { vehicleId, plate, locked, source } | A car is locked or unlocked by anything; source when a player did it |
whip_core:vehicle:spawned | { vehicleId, entity, netId, plate, model, session, temp? } | An owned car comes out, or a temp car is made (temp = true) |
whip_core:vehicle:despawned | { vehicleId, netId } | An owned car leaves the world; the record stays. Its next spawn has a new netId |
whip_core:vehicle:released | { vehicleId, reason, temp } | WHIP lets go of a street car or removes a temp car; the vehicleId then means nothing. reason: 'expired', 'idle', 'gone', 'stopped', 'despawned', 'deleted', 'released' (ReleaseTag) or 'failed' |
whip_core:vehicle:registered | { vehicleId, plate, model, session, source } | WHIP takes on a street car (driven, lockpicked, at a pump, AdoptVehicle); session is always true, source the reporting player if any |
whip_core:vehicle:claimed | { vehicleId, from, ownerCharId } | A street car becomes owned; from is its session id |
whip_core:vehicle:plateChanged | { vehicleId, from, to } | A car's plate changed |
whip_core:vehicle:lockpicked | { vehicleId, plate, source } | A player lockpicked a car open |
AddEventHandler('whip_core:vehicle:lockpicked', function(data)
local coords = GetEntityCoords(GetPlayerPed(data.source))
TriggerClientEvent('dispatch:alert', -1, 'Vehicle Break-In', coords)
end)Full lists: Server Events and Client Events.
Hearing a stat change#
A stat changing has no event. On the server, OnVehicleStatChanged calls you when a named stat moves on any car, whoever registered it. It's called on a change, not on a clock: "full for ten seconds" needs your own timer.
-- A coolant add-on reacting to whip_autos' boost
exports.whip_core:OnVehicleStatChanged('boost', function(vehicleId, value, previous)
if value >= 20 then --[[ flat out ]] end
end)