👻Exports - Adding XP

Description

The AddExp export allows you to add experience points to a player's crafting level. The function automatically handles level progression, respects maximum level limits, and updates the database accordingly.

Syntax

exports['koja-crafting']:AddExp(data)

Parameters

Parameter
Type
Required
Description

data

table

Yes

Data table containing player information and EXP amount

data.identifier

string

Optional*

Player's identifier (steam:, license:, etc.)

data.playerId

number

Optional*

Player's server ID

data.exp

number

Yes

Amount of experience points to add

Note: You must provide either identifier OR playerId. If both are provided, identifier takes priority.

Return Value

This function does not return any value.

Usage Examples

Example 1: Add EXP using player identifier

exports['koja-crafting']:AddExp({
    identifier = 'license:110000103fd1bb1',
    exp = 100
})

Example 2: Add EXP using player server ID

exports['koja-crafting']:AddExp({
    playerId = 1,
    exp = 50
})

Example 3: Reward player for completing a task

RegisterServerEvent('myResource:taskCompleted')
AddEventHandler('myResource:taskCompleted', function()
    local src = source
    
    exports['koja-crafting']:AddExp({
        playerId = src,
        exp = 25
    })
end)

Example 4: Add EXP from another functions function (needs a export or an event)

-- In your resource's server-side script
local function RewardCraftingExp(playerSource, expAmount)
    exports['koja-crafting']:AddExp({
        playerId = playerSource,
        exp = expAmount
    })
end

-- Usage
RewardCraftingExp(1, 150)

Behavior

Level Progression

  • When a player gains enough EXP, they automatically level up

  • The function handles multiple level-ups in a single call if sufficient EXP is provided

  • EXP overflow carries over to the next level

Maximum Level

  • If a player is already at maximum level, the function returns without adding EXP

  • EXP is capped at the maximum allowed for the current level when at max level

Database Updates

  • Player level and EXP are automatically saved to the database

  • Uses the koja-crafting database table

Error Handling

The function includes built-in error handling for the following scenarios:

Scenario
Behavior

Invalid playerId provided

Logs error if Config.Debug is enabled, function returns

No identifier or playerId provided

Logs error if Config.Debug is enabled, function returns

Player data not found

Logs error if Config.Debug is enabled, function returns

Player at maximum level

Logs message if Config.Debug is enabled, function returns

Debug Messages

When Config.Debug is enabled, the function logs:

  • Player already at max level warnings

  • Database update confirmations with current level and EXP

  • Error messages for missing player data or invalid parameters

  • Configuration - Configure level progression and maximum levels

  • Commands - Use the /addexp command for manual EXP addition

Last updated