Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
Pass PlayerPedId() to event and action callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobius1 committed Apr 25, 2022
1 parent cf4f69d commit c0fbd2d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ TriggerClientEvent('xperience:client:setRank', playerId --[[ integer ]], rank --

Listen for rank up event
```lua
AddEventHandler("experience:client:rankUp", function(newRank, previousRank)
AddEventHandler("experience:client:rankUp", function(newRank, previousRank, player)
-- do something when player ranks up
end)
```

Listen for rank down event
```lua
AddEventHandler("experience:client:rankDown", function(newRank, previousRank)
AddEventHandler("experience:client:rankDown", function(newRank, previousRank, player)
-- do something when player ranks down
end)
```
Expand All @@ -151,9 +151,10 @@ Config.Ranks = {
[1] = { XP = 0 },
[2] = {
XP = 800, -- The XP required to reach this rank
Action = function(rankUp, prevRank)
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 },
Expand Down Expand Up @@ -213,9 +214,8 @@ Example of giving a minigun with `500` bullets to a player for reaching rank `10

#### Rank Event
```lua
AddEventHandler("experience:client:rankUp", function(newRank, previousRank)
AddEventHandler("experience:client:rankUp", function(newRank, previousRank, player)
if newRank == 10 then
local player = PlayerPedId()
local weapon = `WEAPON_MINIGUN`

if not HasPedGotWeapon(player, weapon, false) then
Expand Down Expand Up @@ -243,9 +243,8 @@ Config.Ranks = {
[9] = { XP = 19800 },
[10] = {
XP = 24000,
Action = function(rankUp, prevRank)
Action = function(rankUp, prevRank, player)
if rankUp then -- only run when player moved up to this rank
local player = PlayerPedId()
local weapon = `WEAPON_MINIGUN`

if not HasPedGotWeapon(player, weapon, false) then
Expand Down
10 changes: 7 additions & 3 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local Xperience = {}

TriggerServerEvent('xperience:server:load')

if Config.UseESX then
AddEventHandler('esx:playerLoaded', function()
TriggerServerEvent('xperience:server:load')
Expand Down Expand Up @@ -39,16 +41,18 @@ end
----------------------------------------------------

function Xperience:OnRankChange(data, cb)
local player = PlayerPedId()

if data.rankUp then
TriggerEvent("experience:client:rankUp", data.current, data.previous)
TriggerEvent("experience:client:rankUp", data.current, data.previous, player)
else
TriggerEvent("experience:client:rankDown", data.current, data.previous)
TriggerEvent("experience:client:rankDown", data.current, data.previous, player)
end

local Rank = Config.Ranks[data.current]

if Rank.Action ~= nil and type(Rank.Action) == "function" then
Rank.Action(data.rankUp, data.previous)
Rank.Action(data.rankUp, data.previous, player)
end

cb('ok')
Expand Down
3 changes: 2 additions & 1 deletion common/ranks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ Config.Ranks = {
[1] = { XP = 0 },
[2] = {
XP = 800, -- The XP required to reach this rank
Action = function(rankUp, prevRank)
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 },
Expand Down

0 comments on commit c0fbd2d

Please sign in to comment.