Secure Events
Any player can send your server any event, with any values, as often as they like. With SecureEvent you write down what a call must look like, and WHIP checks it before your handler runs.
A call that fails never reaches your code, and the Monitor shows who sent it and why. SecureCallback does the same for a question the client waits on.
Before and after
A purchase with RegisterNetEvent, checked by hand:
local lastBuy = {}
RegisterNetEvent('myshop:buy', function(itemKey, amount)
local source = source
local player = exports.whip_core:GetPlayer(source)
if not player or player.isDead then return end -- loaded, and alive
local now = GetGameTimer() -- a rate limit, and a table
if lastBuy[source] and now - lastBuy[source] < 500 then return end -- to clean up when they leave
lastBuy[source] = now
if type(itemKey) ~= 'string' or not itemKey:match('^[%w_]+$') then return end
amount = tonumber(amount)
if not amount or amount ~= math.floor(amount) or amount < 1 or amount > 100 then return end
local pos = GetEntityCoords(GetPlayerPed(source)) -- at the counter
if #(pos - COUNTER) > 5.0 then return end
buy(source, player, itemKey, amount) -- every return above was silent: nobody sees why
end)The same purchase as a SecureEvent:
exports.whip_core:SecureEvent('myshop:buy', {
rateLimit = 500,
params = {
{'itemKey', 'key', 60}, -- letters, digits, _ and -
{'amount', 'amount', 100}, -- a whole number from 1 to 100
},
near = { at = COUNTER, range = 5.0 },
}, function(source, player, itemKey, amount)
buy(source, player, itemKey, amount) -- loaded, alive, checked, at the counter
end)What WHIP checks for you
| Check | What it stops | You write |
|---|---|---|
| A loaded character | Calls from character select, or from someone logging out | Nothing: on by default |
| Alive | A dead player buying, looting or using items | Nothing: on by default |
| How often | A call fired hundreds of times a second | Nothing (10 in a row, then one every 100 ms), or rateLimit |
| Every value | The wrong type, a negative amount, text where a number goes | params |
| Where they are | Buying from across the map | near |
| What they own | Naming somebody else's slot, stash or car | The slot, addr and vehicle types |
| Who they are | Admin and staff actions from everyone else | ace or permission |
What your handler saves lands in one transaction (Save system), but return false or an error doesn't undo it: refund it yourself.
near trusts the position the player's game reports, so teleports, god mode and aimbots still need an anticheat. And never let a client handler decide anything that matters: a cheat can fire those too.Writing one#
Pass the event's name, its rules (the schema) and your handler. A tip jar for the coffee stand from Your First Resource:
-- server.lua
Whip.register(function()
Whip.core:SecureEvent('mycafe:tip', {
rateLimit = 2000, -- one tip every 2 seconds
params = { {'amount', 'amount', 500} }, -- $1 to $500
near = { at = Config.Counter, range = 3.0 },
}, function(source, player, amount)
local receipt, err = exports.whip_core:ChargePlayer(source, amount, { reason = 'Tip' })
if not receipt then return false, err end -- refused: they are shown why
exports.whip_core:NotifyPlayer(source, ('Thanks for the tip, %s'):format(player.firstName), 'success')
end)
end)
-- client.lua: sent like any other event
TriggerServerEvent('mycafe:tip', 20)- Declare it in
Whip.register, so it survives a whip_core restart (Your First Resource, step 3). Later examples show the call alone. - Start the name with your resource's and no other resource can take it.
- The handler gets
source,player, then your values inparamsorder. Use thissource, not the global.playeris a copy of the player sheet.
Send TriggerServerEvent('mycafe:tip', -50) and /whip → System → Monitor shows it refused: amount: Amount must be a whole number of 1 or more. It keeps the last 500 accepted and 500 refused calls until whip_core restarts (Dev Tools). The console prints refusals too, unless Config.LogSecureRejections = false.
Checking the values: params#
One line per value the client sends, in order: a name, a type, then its limits, as in {'amount', 'amount', 100}. Only listed values reach your handler; one you leave out arrives as nil.
- Refused, never corrected:
amount(counts and money),idandkey(a thing named by a number or a word; they check the form, not that it exists), andslot,vehicleandaddr(below). - Softer, for settings:
numberandintpull a value into range;enum,boolandstringfall back to adefaultif you give one. - Shapes:
list,record(a form's fields), andplayer(another player nearby).
Numbers sent as text count as numbers, except in an enum. Every rule is in the types table.
Checking what they own: slot, vehicle, addr#
These check the player may use the thing, and hand your handler the thing itself.
slot: a slot in the caller's pockets, sent as a number or{ sub = 'accessory', slot = 3 }. An empty slot is refused unlessallowEmpty = true; withitem, so is one holding something else. The handler gets{ addr, item, amount, metadata }.vehicle: a car they own, sent as itsvehicleId(not the entity or netId). The handler gets aGetVehiclesnapshot.anyOwner = trueaccepts anyone's owned car (for refuelling, withnear);session = truealso accepts unowned street cars.addr: a slot in a store they can reach, like an open stash. Every item move in WHIP passes this gate (Addresses).
exports.whip_core:SecureEvent('mykit:install', {
params = { {'kit', 'slot', { item = 'mykit_turbo' }}, {'vehicle', 'vehicle'} },
near = { at = 'vehicle', range = 5.0 },
}, function(source, player, kit, v)
local taken, why = exports.whip_core:StoreTake(kit.addr, 1, { reason = 'fitted to ' .. v.plate })
if not taken then return false, why end
exports.whip_core:SetVehicleData(v.vehicleId, 'mykit_turbo', { fittedAt = os.time() })
end)Refusals a real player can meet ("That item is not there any more") are shown to them; forged values, like someone else's pocket, are refused silently.
Checking where they are: near#
near refuses a call unless the player is within range metres (default 5) of at, which is a fixed vector3 or { x, y, z }; 'vehicle', the schema's vehicle value (sitting in it counts); or a function of the checked values:
near = {
range = 15.0,
at = function(source, player, shopId)
local shop = exports.whip_core:GetShop(shopId)
if not shop then return nil end -- refused as too far
return shop.x, shop.y, shop.z -- or a vector3 or { x, y, z }
end,
message = 'You are too far from the shop', -- default 'Too far away'; false for timer calls
},allowUnknown = true lets a call through when at finds no position, and range can be a function, to follow a setting.
How often: rateLimit and burst#
| You write | A player may send |
|---|---|
| nothing | 10 in a row, then one every 100 ms |
rateLimit = 2000 | One, then one every 2 seconds: a cooldown |
rateLimit = 2000, burst = 5 | 5 in a row, then one every 2 seconds |
rateLimit = false | Any number |
A pause earns calls back, up to the burst. Give a burst to anything done quickly by hand, and to anything that closes something: a refused close leaves it open.
Who may send it#
- Dead players are refused unless
whileDead = true(closing a screen, respawn). The console line for that refusal nameswhileDead, and so does a toast with developer mode on. ace = 'myresource.admin': that ACE only, dead or alive.whip.devtoolsalso needs developer mode on.permission = 'mygarage.manage': that permission only.requirePlayer = false: also before a character loads;playeris thennil.
Saying no#
| Your handler returns | The player sees | The Monitor records |
|---|---|---|
false, 'You need a wrench' | The message, as an error | Refused, with the message |
false | Nothing | Refused |
| anything else, or nothing | Only what you send them | Accepted |
To record a reason without showing one, call RecordEventRejection, then return.
Asking the server: SecureCallback#
For a question the client waits on, register a SecureCallback and ask it with Callback. Your handler returns the answer.
local PRICES = { mycafe_coffee = 15, mycafe_muffin = 8 } -- the server's prices, never the client's
-- server.lua, inside Whip.register
Whip.core:SecureCallback('mycafe:getPrice', {
rateLimit = 500,
params = { {'itemKey', 'key', 60} },
}, function(source, player, itemKey)
local price = PRICES[itemKey]
if not price then return false, 'Not sold here' end -- not nil: that would lose the why
return price
end)
-- client.lua, in a command or event handler
local price, why = exports.whip_core:Callback('mycafe:getPrice', 'mycafe_coffee')
if not price then return exports.whip_core:Notify(why, 'error') endThe answer arrives up to its first nil, so refuse with false, why. With no answer, the client gets false and a reason:
| The reason | When |
|---|---|
'Rate limited', 'Player is dead', 'No player session', 'Too far away', 'itemKey: invalid key' … | WHIP's checks refused the call |
| Yours | Your handler returned false, 'Not sold here' |
'Server error' | Your handler errored (see the server console) |
'Unknown callback' | No callback has that name |
'That is no longer available' | Its resource has stopped |
'No answer from the server' | Nothing came back within 10 seconds |
So answer an ordinary "no" with a word or a table, not false. The Monitor records every answer as accepted; RecordEventRejection records a refusal.
Less common options
ownerOf, onReject and sheet = 'light' are on the SecureEvent card. A handler that takes a lock can use TrackInvocationLock.
Common mistakes#
- Registering straight from the top of a file, not in
Whip.register: it can run before whip_core is ready, and a whip_core restart forgets it. - Using the global
sourcein a handler or its helpers. intornumberfor money or a count: they pull-50into range. Useamountwith amax.- A callback returning
nilto refuse: returnfalse, 'why'.
Coming from ESX, QBCore or ox_lib#
| You use today | In WHIP |
|---|---|
RegisterNetEvent + AddEventHandler | SecureEvent |
ESX.RegisterServerCallback, QBCore.Functions.CreateCallback, lib.callback.register | SecureCallback: return the answer, no cb |
ESX.TriggerServerCallback, QBCore.Functions.TriggerCallback, lib.callback.await | Callback: waits, and returns the answer |
Moving an event over: swap the call, list its values in params, delete your own checks, declare it in Whip.register, and treat return false as "refused". You can move one event at a time. The rest: Coming from QBCore, ESX or ox.