Dealership Creator — 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
falseto stop it. - After events run once it has happened. Use them for logs, XP, rewards, taxes, MDTs or phone apps.
All hooks run on the server.
| Where | For |
|---|---|
bridge/hooks/server.lua | Server owners. This file is not escrowed and has an empty function for every hook. Fill in the ones you need. |
exports.fwb_dealerships:registerHook(...) and AddEventHandler('fwb_dealerships:...') | 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.buy(data)
if data.model == 'rhino' then
return false, 'This one is not for sale to civilians' -- the player sees this text
end
return true
endFrom another resource
lua
local id = exports.fwb_dealerships:registerHook('buy', function(data)
if not exports.my_licenses:Has(data.source, 'driver') then
return false, 'You need a driving licence first'
end
end, {
shopFilter = { 'pdm' }, -- only these dealerships (their id)
modelFilter = { 'adder', 't20' }, -- only these cars (spawn names)
print = true, -- print each time it runs (for testing)
})
exports.fwb_dealerships:removeHooks(id) -- that hook
exports.fwb_dealerships:removeHooks() -- every hook of your resourceHow they run
| Rule | |
|---|---|
| Order | The open file first, then registered hooks in the order they were added. |
| Stopping | The 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 on | Returning nothing or true lets it go on. |
| Errors | A hook that throws an error lets the action go on, and the error is printed. |
| Money | buy, financePay, buyShop, stockOrder and truckTake run before any money is taken, so stopping them never costs anything. |
| Clean-up | When your resource stops, its hooks are removed. |
| Speed | Keep hooks quick: they run inside the action. The console warns when one takes longer than 100 ms. |
Before Hooks
| Hook | When | data |
|---|---|---|
buy | Before a car is paid: in full, financed, or through an employee's sale slip | source, identifier, name, shop, shopLabel, model, label, price (car + paint), payNow (taken now), method (cash / bank), paid (cash / bank / finance / society / free), financed, seller |
spawnVehicle | Before the bought car appears at the delivery spot. false = it stays in the player's garage (it is already theirs). | source, shop, model, label, plate |
testDrive | Before any test drive starts (alone, with friends, or an employee's drive) | source (driver), shop, model, label, seconds, people (server ids) |
financePay | A player pays ahead in My finance (timed payments don't ask) | source, contract, plate, model, amount, method, payOff |
buyShop | A player buys a dealership from the city or from its owner | source, shop, shopLabel, price, method, from (city / player), seller |
stockOrder | A boss or employee orders stock (the shop account pays) | source, shop, cars ({ model, copies }), copies, cost |
truckTake | A driver takes a truck delivery | source, shop, order, from (board / pickup) |
Examples
lua
-- cars above 500 000 only for players with a VIP role
function before.buy(data)
if data.price > 500000 and not exports.my_vip:IsVip(data.source) then
return false, 'This car is for VIP members'
end
end
-- no test drives at night
function before.testDrive(data)
local hour = tonumber(os.date('%H'))
if hour < 6 then return false, 'Test drives start at 6 in the morning' end
endAfter Events
Each one runs after.<event>(data) in the open file, then fires the server event fwb_dealerships:<event>.
| Event | When | data |
|---|---|---|
vehicleBought | A car was sold, paid and written to the player's vehicles | source, identifier, name, shop, shopLabel, model, label, plate, price, payNow, paid, financed, seller, commission |
vehicleSpawned | The bought car appeared with the buyer in it (keys given) | source, shop, model, label, plate, entity, netId |
testDriveStarted | A test drive started | source, shop, model, label, seconds |
testDriveEnded | A test drive ended (time, left, area, staff, damaged) | source, shop, model, label, reason |
financePaid | A finance payment was paid (timed, or ahead) | source, identifier, contract, plate, model, amount, paidOff, ahead |
financeMissed | A timed payment failed | source, identifier, contract, plate, model, missed, left |
vehicleRepossessed | A financed car was taken back | source (nil when offline), identifier, contract, plate, model |
shopBought | Someone bought a dealership | source, identifier, name, shop, shopLabel, price, from, seller |
stockOrdered | Stock was ordered | source, shop, cars, cost |
truckDelivered | A truck driver delivered a load and was paid | source, shop, order, cars, pay, deposit, trip, trips |
Examples
lua
-- give the buyer a registration paper
AddEventHandler('fwb_dealerships:vehicleBought', function(data)
exports.ox_inventory:AddItem(data.source, 'car_papers', 1, { plate = data.plate, model = data.label })
end)
-- a full tank in every car driven out of the dealership
function after.vehicleSpawned(data)
Entity(data.entity).state:set('fuel', 100, true)
end