Koja Scripts
KOJA-LIB

Callbacks

The callback system allows server scripts to handle requests from clients and vice versa.

Callbacks

The callback system allows server scripts to handle requests from clients and vice versa.

RegisterServerCallback

Register a callback handler on the server that clients can trigger.

KOJA.Server.RegisterServerCallback(key, handler)
ParameterTypeDescription
keystringCallback identifier
handlerfunction(source, payload, cb)Handler — call cb(result) to respond

Example

KOJA.Server.RegisterServerCallback('myScript:getShopItems', function(source, payload, cb)
    local items = MySQL.query.await('SELECT * FROM shop_items WHERE category = ?', { payload.category })
    cb(items)
end)

Triggered from the client:

KOJA.Client.TriggerServerCallback('myScript:getShopItems', { category = 'food' }, function(items)
    for _, item in ipairs(items) do
        print(item.name, item.price)
    end
end)

TriggerClientCallback

Trigger a callback registered on a specific client from the server.

KOJA.Server.TriggerClientCallback(key, playerId, payload, callback)
ParameterTypeDescription
keystringCallback identifier
playerIdnumberTarget player server ID
payloadanyData to send
callbackfunction(...)Called with the client's response

Example

KOJA.Server.TriggerClientCallback('myScript:getLocalCoords', source, nil, function(coords)
    print('Player is at:', coords.x, coords.y, coords.z)
end)

Registered on the client:

KOJA.Client.RegisterClientCallback('myScript:getLocalCoords', function(payload, cb)
    local pos = GetEntityCoords(PlayerPedId())
    cb({ x = pos.x, y = pos.y, z = pos.z })
end)

TriggerCallback (internal)

Manually trigger an already-registered server callback from another server script.

KOJA.Server.TriggerCallback(key, source, payload, cb)

This is mainly for internal use. Prefer RegisterServerCallback + client triggers for the standard pattern.

  • Police — Functions for checking how many police officers are online.
  • Licenses — Check whether a player holds a specific driving or skill license.
  • Money — Functions for reading and modifying a player's money on the server.
  • Inventory — Server-side inventory functions.
  • Notifications — Send notifications to a player from the server side.
  • Player — Functions for reading player information on the server.
  • Server API — back to the section overview