Skip to content

FWB Vehicle Keys — Hooks & Events ​

Hooks let you add your own rules to the script, or react when something happens, without editing the escrowed code. They work like ox_inventory's registerHook.

  • Before hooks run before something happens. Return false to stop it.
  • After events run once it has happened. Use them for logs, XP, rewards, MDTs or phone apps.

All hooks run on the server.

WhereFor
bridge/hooks/server.luaServer owners. This file is not escrowed and has an empty function for every hook. Fill in the ones you need.
exports.fwb_vehiclekeys:registerHook(...) and AddEventHandler('fwb_vehiclekeys:...')Other resources

NOTE

Developer note: these open functions are for your own rules and integrations. FWB Studio does not support third-party code written inside them.


Stopping Something ​

In bridge/hooks/server.lua ​

lua
function before.lockpick(data)
    if data.plate:sub(1, 4) == 'LSPD' then
        return false, 'This car is too well protected'   -- the player sees this text
    end
    return true
end

From another resource ​

lua
local id = exports.fwb_vehiclekeys:registerHook('lockpick', function(data)
    if exports.my_heists:IsLockdown() then return false, 'The city is on lockdown' end
end, {
    plateFilter = { 'LS*', 'GOV*' },   -- only these plates (* = anything)
    modelFilter = { 'police', 'fbi' }, -- only these models (spawn names or hashes)
    print = true,                      -- print each time it runs (for testing)
})

exports.fwb_vehiclekeys:removeHooks(id)   -- that hook
exports.fwb_vehiclekeys:removeHooks()     -- every hook of your resource

How they run ​

Rule
OrderThe open file first, then registered hooks in the order they were added.
StoppingThe first hook that returns false stops it. Its second return value is shown to the player; without one they see "You cannot do that right now".
Going onReturning nothing or true lets it go on.
ErrorsA hook that throws an error lets the action go on, and the error is printed.
MoneyHooks run before any fee is taken, so stopping an action never costs the player money.
Clean-upWhen your resource stops, its hooks are removed.
SpeedKeep hooks quick: they run inside the action. The console warns when one takes longer than 100 ms.
ExportsExports from other scripts skip the player-action hooks. giveKey and removeKey always run.

Before Hooks ​

Every data has source (the player's server id). It has plate, entity (the car) and netId when there is a car.

HookWhendata
giveKeyA key is given, in any way: locksmith, spare, found, carjacked, scripts, adminsource, plate, kind, seconds, label, by
removeKeyA key is taken awaysource, plate, identifier
canDriveSomeone with a key sits in the driver seat. false = they can't start it.source, plate, entity, netId, kind
toggleLockThe lock key, or the fob's lock / unlocksource, plate, entity, netId, locked (new state), from = 'key' / 'fob'
remoteAny other fob or key ring buttonsource, plate, entity, netId, action, part
lockpickA lockpick is used on a locked carsource, plate, entity, netId, item, advanced
hotwireHotwiring from the driver seatsource, plate, entity, netId
searchCarSearching a car for its keysource, plate, entity, netId
searchBodySearching a dead NPC driversource, plate, entity, netId, body
carjackAn NPC driver was threatened long enoughsource, plate, entity, netId, driver
giveSpareThe owner gives a spare to a nearby playersource, target, plate
takeBackThe owner takes spares backsource, plate, identifier (nil = all)
handoverThe owner hands the car to a nearby playersource, target, plate, fee
killSwitchThe owner's kill switchsource, plate
locksmithA locksmith servicesource, service (newKey / spare / changeLocks), plate, price
locksmithShopBuying a tracker, detector or plierssource, what, item, price
changePlateA new number plate at the locksmithsource, oldPlate, newPlate, price
plantTrackerPlanting a tracker (own = the owner's own)source, plate, entity, netId, own
scanCarScanning a car with the detectorsource, plate, entity, netId
cutTrackerCutting a found tracker offsource, plate, entity, netId, trackerId

remote actions: trunk, engine, lights, panic, summon, track, window, windows, door, doors, hazard, interior.

Examples ​

lua
-- only the police job may drive cars with LSPD plates
function before.canDrive(data)
    if data.plate:sub(1, 4) ~= 'LSPD' then return true end
    local job = exports.my_jobs:GetJob(data.source)
    return job == 'police', 'Police cars are for officers'
end

-- no carjacking inside the city's green zone
function before.carjack(data)
    if exports.my_zones:IsSafe(GetEntityCoords(data.entity)) then
        return false, 'Not in a safe zone'
    end
end

After Events ​

lua
-- in bridge/hooks/server.lua
function after.carjacked(data)
    print(('%s carjacked %s: the driver chose %s'):format(GetPlayerName(data.source), data.plate, data.outcome))
end
lua
-- any other resource (server side)
AddEventHandler('fwb_vehiclekeys:lockpicked', function(data)
    if data.result == 'open' then exports.my_xp:Add(data.source, 'thief', 5) end
end)
Eventdata
keyGivensource, plate, kind, seconds, label, by
keyRemovedsource, plate, identifier
locksChangedplate, version, ownerSource (every older key stopped working)
lockChangedsource (nil when a script did it), plate, entity, netId, locked
lockpickedsource, plate, entity, netId, result (open / broke / failed), advanced
hotwiredsource, plate, entity, netId, result (hot / failed)
carSearchedsource, plate, entity, netId, found
bodySearchedsource, plate, entity, netId, found
carjackedsource, plate, entity, netId, outcome (keys / flee / drive / fight)
spareGivensource, target, plate
carHandedOversource, target, plate, fee
trackerPlantedsource, plate, id, own
trackerRemovedsource (nil: admin or script), plate, id, own
engineKilledsource, plate
plateChangedFires as fwb_vehiclekeys:plateChanged with (oldPlate, newPlate). In the open file: after.plateChanged({ source, oldPlate, newPlate }).

The event name is always fwb_vehiclekeys: plus the name in the table. These are server events only, so a player's game can't fake them.

FWB Studio documentation.