Code Monkey home page Code Monkey logo

xperience's Introduction

xperience

XP Ranking System for FiveM

  • Designed to emulate the native GTA:O system
  • Saves and loads players XP / rank
  • Add / remove XP from your own script / job
  • Allows you listen for rank changes to reward players
  • Fully customisable UI
  • Framework agnostic, but supports ESX and QBCore
Increasing XP

Demo Image 1

Rank Up

Demo Image 2

Table of Contents

Install

Select an option:

  • Option 1 - If you want to use xperience as a standalone resource then import xperience_standalone.sql only
  • Option 2 - If using ESX with Config.UseESX set to true then import xperience_esx.sql only. This adds the xp and rank columns to the users table
  • Option 3 - If using QBCore with Config.UseQBCore set to true then there's no need to import any sql files as the xp and rank are saved to the player's metadata - see QBCore Integration

then:

  • Drop the xperience directory into you resources directory
  • Add ensure xperience to your server.cfg file

By default this resource uses oxmysql, but if you don't want to use / install it then you can use mysql-async by following these instructions:

  • Uncomment the '@mysql-async/lib/MySQL.lua', line in fxmanifest.lua and comment out the '@oxmysql/lib/MySQL.lua' line

Transitioning from esx_xp

If you previously used esx_xp and are still using es_extended then do the following to make your current stored xp / rank data compatible with xperience

  • Rename the rp_xp column in the users table to xp
  • Rename the rp_rank column in the users table to rank
  • Set Config.UseESX to true

Usage

Client Side

Client Exports

Give XP to player

exports.xperience:AddXP(xp --[[ integer ]])

Take XP from player

exports.xperience:RemoveXP(xp --[[ integer ]])

Set player's XP

exports.xperience:SetXP(xp --[[ integer ]])

Set player's rank

exports.xperience:SetRank(rank --[[ integer ]])

Get player's XP

exports.xperience:GetXP()

Get player's rank

exports.xperience:GetRank()

Get XP required to rank up

exports.xperience:GetXPToNextRank()

Get XP required to reach defined rank

exports.xperience:GetXPToRank(rank --[[ integer ]])

Client Events

Listen for rank up event on the client

AddEventHandler("xperience:client:rankUp", function(newRank, previousRank, player)
    -- do something when player ranks up
end)

Listen for rank down event on the client

AddEventHandler("xperience:client:rankDown", function(newRank, previousRank, player)
    -- do something when player ranks down
end)

Server Side

Server Exports

Get player's XP

exports.xperience:GetPlayerXP(playerId --[[ integer ]])

Get player's rank

exports.xperience:GetPlayerRank(playerId --[[ integer ]])

Get player's required XP to rank up

exports.xperience:GetPlayerXPToNextRank(playerId --[[ integer ]])

Get player's required XP to reach defined rank

exports.xperience:GetPlayerXPToRank(playerId --[[ integer ]], rank --[[ integer ]])

Server Triggers

TriggerClientEvent('xperience:client:addXP', playerId --[[ integer ]], xp --[[ integer ]])

TriggerClientEvent('xperience:client:removeXP', playerId --[[ integer ]], xp --[[ integer ]])

TriggerClientEvent('xperience:client:setXP', playerId --[[ integer ]], xp --[[ integer ]])

TriggerClientEvent('xperience:client:setRank', playerId --[[ integer ]], rank --[[ integer ]])

Server Events

RegisterNetEvent('xperience:server:rankUp', function(newRank, previousRank)
    -- do something when player ranks up
end)

RegisterNetEvent('xperience:server:rankDown', function(newRank, previousRank)
    -- do something when player ranks down
end)

Rank Actions

You can define callbacks on each rank by using the Action function.

The function will be called both when the player reaches the rank and drops to the rank.

You can check whether the player reached or dropped to the new rank by utilising the rankUp parameter.

Config.Ranks = {
    [1] = { XP = 0 },
    [2] = {
        XP = 800, -- The XP required to reach this rank
        Action = function(rankUp, prevRank, player)
            -- rankUp: boolean      - whether the player reached or dropped to this rank
            -- prevRank: number     - the player's previous rank
            -- player: integer      - The current player            
        end
    },
    [3] = { XP = 2100 },
    [4] = { XP = 3800 },
    ...
}

QBCore Integration

If Config.UseQBCore is set to true then the player's xp and rank are stored in their metadata. The metadata is saved whenever a player's xp / rank changes.

Client

local PlayerData = QBCore.Functions.GetPlayerData()
local xp = PlayerData.metadata.xp
local rank = PlayerData.metadata.rank

Server

local Player = QBCore.Functions.GetPlayer(src)
local xp = Player.PlayerData.metadata.xp
local rank = Player.PlayerData.metadata.rank

ESX Integration

Server

local xPlayer = ESX.GetPlayerById(src)
local xp = xPlayer.get('xp')
local rank = xPlayer.get('rank')

Commands

-- Set the theme
/setXPTheme [theme]

Admin Commands

These require ace permissions: e.g. add_ace group.admin command.addXP allow

-- Award XP to player
/addXP [playerId] [xp]

-- Deduct XP from player
/removeXP [playerId] [xp]

-- Set a player's XP
/setXP [playerId] [xp]

-- Set a player's rank
/setRank [playerId] [rank]

Themes

The theme can be set by the player using the /setXPTheme [theme] command. The theme argument must exist in the Config.Themes table in config.lua for it to work:

Config.Theme  = 'native'  -- Set the default theme (must exist in the Config.Themes table)
 
Config.Themes = {
    native = {
        segments = 10,  -- Sets the number of segments the XP bar has. Native = 10, Max = 20
        width = 532     -- Sets the width of the XP bar in px
    },

    hitman = {
        segments = 80,
        width = 800
    },
    
    hexagon = {
        segments = 16,
        width = 400
    },
}

Custom Themes

Let's say you want to add a theme called myTheme:

  • Add the theme table to the Config.Themes table using the name of the theme as the index:
Config.Themes = {
    ...
    
    myTheme = {
        segments = 20,
        width = 650
    }
}
  • Create the theme's .css file in ui/css directory with the theme- prefix:
ui/css/theme-myTheme.css
  • Set Config.Theme to read your new theme:
Config.Theme = 'myTheme'

Markup for making themes:

<div class="xperience">
    <div class="xperience-inner">
        <div class="xperience-rank">
            <div>XXXX</div> <!-- CURRENT RANK -->
        </div>
        <div class="xperience-progress"> <!-- MAIN PROGRESS BAR -->
            <div class="xperience-segment"> <!-- BAR SEGMENT (IF YOU'VE SET THE THEME'S SEGMENTS TO 10 THEN THERE'LL BE 10 OF THESE) -->
                <div class="xperience-indicator--bar"></div> <!-- SEGMENT INDICATOR (ONLY USED WHEN XP IS UPDATING)-->
                <div class="xperience-progress--bar"></div> <!-- SEGMENT PROGRESS -->
            </div>
            ...
        </div>
        <div class="xperience-rank">
            <div>XXXX</div> <!-- NEXT RANK -->
        </div>
    </div>
    <div class="xperience-data">
        <span>XXXX</span> <!-- CURRENT XP -->
        <span>XXXX</span> <!-- XP REQUIRED FOR NEXT RANK -->
    </div>
</div>

FAQ

How do I award players XP for X amount of playtime?

Example of awarding players 100XP for every 30mins of playtime

-- Server side
CreateThread(function()
    local interval = 30   -- interval in minutes
    local xp = 100        -- XP amount to award every interval

    while true do
        for i, src in pairs(GetPlayers()) do
            TriggerClientEvent('xperience:client:addXP', src, xp)
        end
        
        Wait(interval * 60 * 1000)
    end
end)

How do I give XP to a player when they've done something?

Example of giving a player 100 XP for shooting another player

AddEventHandler('gameEventTriggered', function(event, data)
    if event == "CEventNetworkEntityDamage" then
        local victim      = tonumber(data[1])
        local attacker    = tonumber(data[2])
        local weaponHash  = tonumber(data[5])
        local meleeDamage = tonumber(data[10]) ~= 0 and true or false 

        -- Don't register melee damage
        if not meleeDamage then
            -- Check victim and attacker are both players
            if (IsEntityAPed(victim) and IsPedAPlayer(victim)) and (IsEntityAPed(attacker) and IsPedAPlayer(attacker)) then
                if attacker == PlayerPedId() then -- We are the attacker
                    exports.xperience:AddXP(100) -- Give player 100 xp for getting a hit
                end
            end
        end
    end
end)

How do I do something when a player's rank changes?

You can either utilise Rank Events or Rank Actions.

Example of giving a minigun with 500 bullets to a player for reaching rank 10:

Rank Event

AddEventHandler("xperience:client:rankUp", function(newRank, previousRank, player)
    if newRank == 10 then
        local weapon = `WEAPON_MINIGUN`
        
        if not HasPedGotWeapon(player, weapon, false) then
            -- Player doesn't have weapon so give it them loaded with 500 bullets
            GiveWeaponToPed(player, weapon, 500, false, false)
        else
            -- Player has the weapon so give them 500 bullets for it
            AddAmmoToPed(player, weapon, 500)
        end
    end
end)

Rank Action

Config.Ranks = {
    [1] = { XP = 0 },
    [2] = { XP = 800 },
    [3] = { XP = 2100 },
    [4] = { XP = 3800 },
    [5] = { XP = 6100 },
    [6] = { XP = 9500 },
    [7] = { XP = 12500 },
    [8] = { XP = 16000 },
    [9] = { XP = 19800 },
    [10] = {
        XP = 24000,
        Action = function(rankUp, prevRank, player)
            if rankUp then -- only run when player moved up to this rank
                local weapon = `WEAPON_MINIGUN`
        
                if not HasPedGotWeapon(player, weapon, false) then
                    -- Player doesn't have weapon so give it them loaded with 500 bullets
                    GiveWeaponToPed(player, weapon, 500, false, false)
                else
                    -- Player has the weapon so give them 500 bullets for it
                    AddAmmoToPed(player, weapon, 500)
                end
            end
        end
    },
    [11] = { XP = 28500 },
    ...
}

License

xperience - XP Ranking System for FiveM

Copyright (C) 2021 Karl Saunders

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>

xperience's People

Contributors

mobius1 avatar sean920310 avatar stevspotted avatar virus18sp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

xperience's Issues

Integration on qbcore

Hey @Mobius1 How are you doing ? I'm looking for a script to integrate into qb-core and you're seems perfect.
I have fews issues with TriggerClientEvent('xperience:client:addXP', source --[[ integer ]], xp --[[ integer ]])
and a error from Init. Do you know what is about ?

Also about the triggerclientevent I always get nil value like this. I'm using it like this :
-- > exports.xperience:AddXP(100) on my client side of the event
--> TriggerClientEvent('xperience:client:addXP', source, 100) on my server side of the event

I am using it the right way ?

Best regards and thank you for help in advance :) @Mobius1 ?
image

image

UI not showing / not loaded

hi , im using old esx .

i try to use esx version , no work
use standlone also no work .

i already tried #4

still no load .

waiting for update .

Not showing up

I installed the script as a standalone variant but when I press Z the UI is not showing up however when I use setXP or addXP then the debug pops up in the console, also the player is not getting recorded in the database I think

xp not working with esx_multicharacter

the script is not working with multicharacter because it triggers the license but on multicharacter it has a prefix before the license. Can you instead make it use getIdentifier or so?

Xperience Mini Leaderboard

Esx xp very usefull and have their leaderboard. How can i add mini leaderboard like tat to this xperience resources too. Pls help thanks.

disable melee on rank 1

i use qb

can you help me make disable melee (like punching) on rank 1, bcs i dont want new player punching everybody on the server D:

i made a thread that check player's rank and if its under 2, it will use disablecontrolaction native, it works if the player has relogged after he registered new character.

the problem is if a new player register on the server freshly, the thread isnt working (its like it give nil value when checking player's rank or smthing else is making it not working) and this new player can still punch until he relogged from the server

Getting error when first running server with xperience

Hi, I am getting the following error when I first start my server
[ script:mysql-async] [MariaDB:10.4.28-MariaDB] [ERROR] [xperience] An error happens for query "SELECT * FROM user_experience WHERE identifier = ? : ["369be8d2e08c3dd24e426991b4b2b3396a5178b0"]": ER_NO_SUCH_TABLE: Table 'fivem.user_experience' doesn't exist
[ script:xperience] SCRIPT ERROR: @xperience/server/main.lua:83: attempt to index a nil value (local 'res')
[ script:xperience] > ref (@xperience/server/main.lua:83)
[ script:xperience] > (@mysql-async/mysql-async.js:15543)
what can i do to fix such?

esx_xp help

I need help how can I put that each level achieved the player gets a reward, to give motivation and missions to do to the players

Infinite xp glitch

I used this as in FAQ

`AddEventHandler('gameEventTriggered', function(event, data)
if event == "CEventNetworkEntityDamage" then
local victim = tonumber(data[1])
local attacker = tonumber(data[2])
local weaponHash = tonumber(data[5])
local meleeDamage = tonumber(data[10]) ~= 0 and true or false

    -- Don't register melee damage
    if not meleeDamage then
        -- Check victim and attacker are both players
        if (IsEntityAPed(victim) and IsPedAPlayer(victim)) and (IsEntityAPed(attacker) and IsPedAPlayer(attacker)) then
            if attacker == PlayerPedId() then -- We are the attacker
                exports.xperience:AddXP(100) -- Give player 100 xp for getting a hit
            end
        end
    end
end

end) `

whenever a player throws gas , same player gains infinite xp until he dies, this causes earning xp to fast. How can I make it ignore gas and tazer?

xperience

hello i have downloaded xperience but apparently it doesn't work correctly, since the experience bar doesn't appear and it doesn't add xp either, since i also tried esx_xp and it works fine, but it is already osboleto as you mentioned. i am using esx legacy 1.8.5

Ui don't show

I added the resource and the config is set. Started the server and it does not show up at all.
I read a similar issue but even that fix does not work.
Help please

UI Does Not Show Up - Nothing Inserted into Database - Commands Don't Work As Intended (Agnostic Setup)

After setting up a local database through MariaDB, setting up HeidiSQL, and after a fresh install of Xperience Version 0.3.2

I am unable to get any portion of this resource to function. Admittedly, this is my first time working with MySQL, but I have verified the connection between OxMySQL and the Database is working.

In other words, the problem appears to be confined to Xperience specifically. Not smart enough with this stuff to nitpick into specifically what went wrong code-wise, but here is the in-game scoop:

  • The UI Does not show up when clicking 'Z'
  • Commands do not function normally despite receiving "PLAYER [my_name] EXECUTED COMMAND setXP" in chat.
  • After a command is triggered, No data is inserted into the database. All table settings match documentation to a T, including collation.
  • Re-verified Syntax & Spelling Numerous Times; None Observed
  • 'ensure oxmysql' comes before 'ensure Xperience' within the resource.cfg file.

Drawing from this encounter and considering the additional issue reports present on this GitHub page, I've decided to step back and refrain from using Xperience until I can confirm whether my novice-level expertise is contributing to the issues. After about 2 days of troubleshooting... brain is jelly πŸ‘―β€β™‚οΈ πŸ‘―β€β™‚οΈπŸ‘―β€β™‚οΈ
image
image
image

question

why

local QBCore = exports['qb-core']:GetCoreObject()
local bonus = 5000

Action = function(rankUp, prevRank, player)
            -- rankUp: boolean      - whether the player reached or dropped to this rank
            -- prevRank: number     - the player's previous rank
            -- player: integer      - The current player
            print(player)

            local src = player
             local Playerr = QBCore.Functions.GetPlayer(src)
             Playerr.Functions.AddMoney('cash', bonus)
        end

doesn't work
I get this error
image

LEADERBOARD

leaderboard hopefully can new features for the next update

HELP

i have a drift system for points on my server how can i make it so people level up from drifting ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.