Examples
Short recipes for patterns the tutorials and guides don't show: repairing on the client after the server agrees, refunding a failed sale, vehicle parameters, reacting to shop events, and calling another WHIP resource.
Whip.register. Secure events, callbacks, actions and items registered straight from the top of a file vanish when whip_core restarts (the satellite helper). AddEventHandler needs neither. Items such as repair_kit aren't shipped: define them first with RegisterItem or Config.Items.Mechanic: repair on the client
A choice on the bonnet's prompt. The server checks and takes the kit; the mechanic's own game repairs, because engine health is a client native that needs network control.
-- client.lua
Whip.register(function()
Whip.core:AddAction({
id = 'repair',
at = { vehiclePart = 'hood' }, -- shows while the bonnet is open
label = 'Repair Engine',
require = { item = 'repair_kit' }, -- only offered to someone carrying one
canInteract = function(ctx) return GetVehicleEngineHealth(ctx.entity) < 900.0 end,
onSelect = function(ctx) TriggerServerEvent('mechanic:repair', VehToNet(ctx.entity)) end,
})
end)
RegisterNetEvent('mechanic:client:repair', function(netId)
local veh = NetToVeh(netId)
if exports.whip_core:TakeVehicleControl({ entity = veh }) then
SetVehicleEngineHealth(veh, 1000.0)
end
end)
-- server.lua
Whip.register(function()
Whip.core:SecureEvent('mechanic:repair', {
rateLimit = 2000,
params = { {'netId', 'id'} }, -- refused if bad, never clamped
near = { -- beside that car: naming it proves nothing
at = function(source, player, netId)
local veh = NetworkGetEntityFromNetworkId(netId)
if DoesEntityExist(veh) then return GetEntityCoords(veh) end
end,
range = 6.0,
},
}, function(source, player, netId)
if not exports.whip_core:RemoveItem(source, 'repair_kit', 1, false, { reason = 'repair' }) then
return false, 'No repair kit!' -- shown to the player, logged as refused
end
TriggerClientEvent('mechanic:client:repair', source, netId)
end)
end)Dealership: refund a failed sale
ChargePlayer returns a receipt; if a later step fails, RefundPlayer returns the money the way it was paid.
function SellVehicle(buyerSource, vehicleId, price)
local receipt, err = exports.whip_core:ChargePlayer(buyerSource, price, { method = 'cash', reason = 'Car sale' })
if not receipt then return false, err end
local ok, why = exports.whip_core:TransferVehicle(vehicleId, buyerSource) -- gives a key by default
if not ok then
exports.whip_core:RefundPlayer(receipt)
return false, why
end
return true
endCreateOwnedVehicle(source, data, { giveKey = true, spawnAt = vector4(...) }) creates it, brings it out and hands over the key, undoing itself if any step fails. Only the payment is yours to undo.Gas station: fuel is a vehicle stat
There's no SetVehicleFuel: fuel is a vehicle stat registered by whip_fuel (which must be running), read and written like any other. The 'vehicle' parameter resolves the id and checks the player owns the car.
local PUMPS = { vector3(49.4, 2778.8, 58.0), vector3(263.9, 2606.5, 44.9) }
Whip.register(function()
Whip.core:SecureEvent('gasstation:refuel', {
rateLimit = 2000,
params = { {'vehicle', 'vehicle'} },
near = { at = 'vehicle', range = 5.0 }, -- at (or in) that car
}, function(source, player, v)
local atPump = false
for _, pump in ipairs(PUMPS) do
if #(v.coords - pump) < 12.0 then atPump = true break end
end
if not atPump then return false, 'Drive up to a pump first' end
local fuel = exports.whip_core:GetVehicleStat(v.vehicleId, 'fuel')
local cost = math.ceil((100 - fuel) * 5) -- $5 per percent
if cost < 1 then return false, 'The tank is already full' end
local receipt, err = exports.whip_core:ChargePlayer(source, cost, { method = 'cash', reason = 'Fuel' })
if not receipt then return false, err end
-- The last argument is the change's source, shown in Vehicle Stats Debug
exports.whip_core:SetVehicleStat(v.vehicleId, 'fuel', 100.0, 'gasstation:refuel')
end)
end)Impound: anyone's car, and lockpick alerts
{ anyOwner = true, session = true } lets the 'vehicle' parameter take any car, street cars included. near = { at = 'vehicle' } checks where the car is now, not its last saved spot.
Whip.register(function()
Whip.core:SecureEvent('impound:seize', {
rateLimit = 2000,
params = { {'vehicle', 'vehicle', { anyOwner = true, session = true }} },
near = { at = 'vehicle', range = 8.0 },
}, function(source, player, v)
if not exports.whip_core:IsPlayerClockedOn(source, 'police') then
return false, 'Only on-duty police can impound'
end
local owner = v.owner.charId and exports.whip_core:GetPlayer({ charId = v.owner.charId })
if owner then exports.whip_core:RemoveVehicleKey(owner.source, v.vehicleId) end
exports.whip_core:SetVehicleLocked(v.vehicleId, true)
end)
end)
AddEventHandler('whip_core:vehicle:lockpicked', function(e)
local coords = GetEntityCoords(GetPlayerPed(e.source))
TriggerClientEvent('dispatch:alert', -1, { title = 'Vehicle Break-In', plate = e.plate, x = coords.x, y = coords.y, z = coords.z })
end)Plate check: re-check the job on the server
An action's require only decides what's offered. The server checks duty again before answering.
-- client.lua: on any car's boot prompt (the plate is at the back)
Whip.register(function()
Whip.core:AddAction({
id = 'run_plate', at = { vehiclePart = 'trunk' }, label = 'Run Plate', icon = 'P',
require = { job = 'police', onDuty = true },
onSelect = function(ctx) TriggerServerEvent('police:runPlate', GetVehicleNumberPlateText(ctx.entity)) end,
})
end)
-- server.lua
Whip.register(function()
Whip.core:SecureEvent('police:runPlate', {
rateLimit = 2000,
params = { {'plate', 'string', 8} },
}, function(source, player, plate)
if not exports.whip_core:IsPlayerClockedOn(source, 'police') then
return false, 'On-duty police only'
end
local v = exports.whip_core:GetVehicle({ plate = plate })
if not v or v.session then return false, plate .. ': no record' end -- street car or rental
exports.whip_core:NotifyPlayer(source, ('%s (%s) is registered to character #%d'):format(plate, v.model, v.owner.charId))
end)
end)Shops: react to purchases and visits
Raise prices as items sell:
local basePrices = { food = 50, drink = 30 }
local sold = {}
AddEventHandler('whip_core:shop:purchased', function(e)
local shop = exports.whip_core:GetShop({ key = 'dynamic_store' })
if not shop or e.shopId ~= shop.shopId then return end
for _, line in ipairs(e.items) do
sold[line.itemKey] = (sold[line.itemKey] or 0) + line.amount
end
local catalog = {}
for key, base in pairs(basePrices) do
catalog[key] = math.floor(base * (1.0 + (sold[key] or 0) * 0.05)) -- +5% per sale
end
exports.whip_core:UpdateShop(shop.shopId, { catalog = catalog })
end)Warn on-duty police when an armed player opens a shop:
AddEventHandler('whip_core:shop:opened', function(e)
local weapon = GetSelectedPedWeapon(GetPlayerPed(e.source))
if weapon == 0 or weapon == GetHashKey('WEAPON_UNARMED') then return end
local shop = exports.whip_core:GetShop(e.shopId)
if not shop then return end
for _, cop in ipairs(exports.whip_core:GetClockedOnEmployees('police') or {}) do -- rows are { charId, source }
exports.whip_core:NotifyPlayer(cop.source, 'Armed customer at ' .. shop.name .. ': possible robbery!', 'warning')
end
end)Weapon shop: another WHIP resource, and Whip.core
Sell a pistol with whip_gunny (add dependency 'whip_gunny'). Once something has waited (GiveWeapon gives through AddItem), call whip_core through Whip.core, which returns even if whip_core stops meanwhile.
local COUNTER = vector3(21.6, -1106.5, 29.8)
-- client.lua
Whip.register(function()
Whip.core:AddAction({
id = 'buy_pistol', at = { coords = COUNTER }, label = 'Buy Pistol ($5000)',
onSelect = function() TriggerServerEvent('gunshop:buyPistol') end,
})
end)
-- server.lua
Whip.register(function()
Whip.core:SecureEvent('gunshop:buyPistol', {
rateLimit = 3000,
near = { at = COUNTER, range = 3.0 },
}, function(source, player)
local receipt, err = exports.whip_core:ChargePlayer(source, 5000, { method = 'cash', reason = 'Pistol' })
if not receipt then return false, err end
local ok, why = exports.whip_gunny:GiveWeapon(source, 'gunny_pistol', false, 'Pistol purchase')
if not ok then
Whip.core:RefundPlayer(receipt)
return false, why
end
exports.whip_gunny:GiveMagazine(source, 'gunny_mag_pistol', 12, 'Pistol purchase')
Whip.core:AddItem(source, 'ammo_9mm', 24, false, 'Pistol purchase')
Whip.core:NotifyPlayer(source, 'Pistol purchased!', 'success')
end)
end)