LGF Utility
Lua
Shared
Events
Callback

Callback Documentation

Registering a Server Callback

---@param callback_name string
---@param callback_function fun(source: number): table
LGF:RegisterServerCallback(callback_name, callback_function)

Description

LGF:RegisterServerCallback() registers a callback function on the server side. This function can be invoked from the client side, allowing the server to process requests and return data to the client.

Parameters

  • callback_name (string): The name of the callback function used to identify and trigger it from the client side.
  • callback_function (function): A function that takes source (the player's source ID) and returns a table with data to be sent back to the client.

Example

local LGF = exports['LGF_Utility']:UtilityData()
 
-- Registering a server callback
LGF:RegisterServerCallback("FetchPlayerData", function(source)
    return {
        source = source,
        playerName = GetPlayerName(source),
        playerIdentifier = GetPlayerIdentifierByType(source, "license")
    }
end)

Triggering a Server Callback

---@param callback_name string
---@return table | string[] | nil
LGF:TriggerServerCallback(callback_name)

Description

LGF:TriggerServerCallback() triggers a registered server callback from the client side. This method sends a request to the server and returns the result of the server-side callback function.

Parameters

  • callback_name (string): The name of the callback function to be triggered, as registered on the server side.

Returns

  • table: The data returned by the server callback function. This can be a table, an array of strings, or nil depending on the callback implementation.

Example

local LGF = exports['LGF_Utility']:UtilityData()
 
-- Triggering the server callback and receiving the response
local response = LGF:TriggerServerCallback("FetchPlayerData")
 
print(response)

Notes

  • Ensure the callback_name used in LGF:TriggerServerCallback() matches the name provided in LGF:RegisterServerCallback().
  • The structure of the returned data depends on the server-side callback implementation and may vary.