Callback
Bidirectional client ↔ server callbacks with await, timeout and automatic cleanup.
Bidirectional client ↔ server callbacks with await, timeout and automatic cleanup.
lib.callback lets one side ask the other for a value and await the answer — with a timeout
and automatic cleanup when a resource stops.
lib.callback.register(name, handler) — only one resource can serve a given name.
lib.callback.register('myscript:getStats', function(source)
return { kills = 3, deaths = 1 }
end)On the server the handler is function(source, ...); on the client it's function(...).
lib.callback.await(name, ...) blocks (async) until the other side replies.
-- call a SERVER callback; the 2nd arg is the throttle (false = none)
local stats = lib.callback.await('myscript:getStats', false)-- call a CLIENT callback; the 2nd arg is the target player id
local coords = lib.callback.await('myscript:getCoords', targetSource)namestringrequiredThe registered callback name.
delaynumber | falseoptionalClient → server only (2nd arg): local throttle in ms. If the same callback fired less than delay ms ago, returns nil immediately — handy against spam clicks. false = no throttle. (Server → client, the 2nd arg is the target playerId instead.)
RegisterNUICallback('submit', function(data, cb)
local result = lib.callback.await('saveData', 500, data)
cb(result or { error = 'throttled' })
end)Default timeout is 5 min (convar see:callbackTimeout) — after that you get false, 'timed out'. Calling an unknown callback throws. A triggered throttle returns nil (not an error) — check for it. Always await inside a CreateThread, never the main thread.