Exports
Loyalty System Complete API Documentation
The Loyalty System provides a set of functions to manage player loyalty cards and points. These functions are exported from the 'Loyalty' resource and can be used in other resources within your QBCore-based server.
Table of Contents
HasCard
GetPoints
AddPoints
RemovePoints
General Notes
Functions
HasCard
Checks if a player has a loyalty card.
Parameters:
playerId(number): The server ID of the player.
Returns:
boolean:trueif the player has a loyalty card,falseotherwise.
Usage:
local hasCard = exports['Loyalty']:HasCard(playerId)
if hasCard then
print("Player has a loyalty card")
else
print("Player does not have a loyalty card")
endGetPoints
Retrieves the current points balance for a player.
Parameters:
playerId(number): The server ID of the player.
Returns:
number: The current points balance of the player. Returns 0 if the player doesn't have a loyalty card.
Usage:
local points = exports['Loyalty']:GetPoints(playerId)
print("Player has " .. points .. " loyalty points")AddPoints
Adds points to a player's loyalty card based on a dollar amount.
Parameters:
playerId(number): The server ID of the player.amount(number): The dollar amount to convert to points.reason(string, optional): A description of why points are being added. Defaults to "Purchase" if not provided.
Returns:
boolean:trueif points were successfully added,falseif the operation failed.string: A message describing the result of the operation.number: The actual number of points added after conversion.
Usage:
local success, message, pointsAdded = exports['Loyalty']:AddPoints(playerId, 50, "Large purchase reward")
if success then
print(message) -- "Points added successfully."
print("Added " .. pointsAdded .. " points")
else
print(message) -- Could be "Player not found." or "Invalid amount. Please provide a valid number."
endNotes:
This function converts the dollar amount to points using
Config.PointsEach1Dollar.The converted amount is rounded down to the nearest integer.
Points addition is logged in the
loyalty_card_logstable.After successful point addition, a notification is sent to the player using the "lb-phone" resource.
If the player is not found or the amount is invalid, the function will return false with an appropriate error message.
RemovePoints
Removes points from a player's loyalty card.
Parameters:
playerId(number): The server ID of the player.pointsToRemove(number): The amount of points to remove.customMessage(string, optional): A custom message for the reason of point removal. If not provided, defaults to "Points removed".
Returns:
boolean:trueif points were successfully removed,falseif the operation failed.string: A message describing the result of the operation.
Usage:
local success, message = exports['Loyalty']:RemovePoints(playerId, 50, "Reward redemption")
if success then
print(message) -- "Points removed successfully."
else
print(message) -- Could be "Player not found." or "Not enough points available."
endNotes:
This function checks if the player has enough points before removing them. If not, the operation will fail.
Point removal is logged in the
loyalty_card_logstable with a negative value forpoints_added.After successful point removal, a notification is sent to the player using the "lb-phone" resource.
General Notes
Server-Side Usage: All these functions should be called server-side.
Resource Dependency: Ensure that the 'Loyalty' resource is started before calling these functions.
Database Interactions:
The system uses a
loyalty_cardstable to store player card information.A
loyalty_card_logstable is used to log all point transactions.
Player Verification: All functions first verify if the player exists using QBCore's
GetPlayerfunction.Notifications: The system uses the "lb-phone" resource to send notifications to players after successful point operations.
Configuration:
The
AddPointsfunction uses a configuration valueConfig.PointsEach1Dollarto convert dollar amounts to points. Ensure this configuration is properly set in your resource.
Error Handling:
Functions return
falsewith an error message if operations fail (e.g., player not found, insufficient points).Always check the return values to handle success and failure cases appropriately in your scripts.
Loyalty Card Requirement:
GetPointswill return 0 if a player doesn't have a loyalty card.AddPointsandRemovePointswill fail if the player doesn't have a loyalty card.Use
HasCardto check for card existence before performing operations if you need to handle card/no-card situations differently.
Asynchronous Operations:
Database operations are performed asynchronously to prevent blocking the main thread.
The
AddPointsandRemovePointsfunctions use MySQL transactions to ensure data integrity.
Rounding: When adding points, the dollar amount is converted to points and rounded down to the nearest integer.
Logging: All point additions and removals are logged in the
loyalty_card_logstable for auditing purposes.
Last updated