Skip to content

Commit

Permalink
Merge pull request #1509 from Kenshiin13/fix-player-death
Browse files Browse the repository at this point in the history
fix(es_extended/client/main): fix player death race condition
  • Loading branch information
Kenshiin13 authored Nov 27, 2024
2 parents ef77e5a + 47ae063 commit 2bfd7ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions [core]/es_extended/client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ ESX.SecureNetEvent("esx:requestModel", function(model)
ESX.Streaming.RequestModel(model)
end)

local function ApplyMetadata(metadata)
if metadata.health then
SetEntityHealth(ESX.PlayerData.ped, metadata.health)
end

if metadata.armor and metadata.armor > 0 then
SetPedArmour(ESX.PlayerData.ped, metadata.armor)
end
end

RegisterNetEvent("esx:playerLoaded", function(xPlayer, _, skin)
ESX.PlayerData = xPlayer

Expand All @@ -34,8 +24,6 @@ RegisterNetEvent("esx:playerLoaded", function(xPlayer, _, skin)

ESX.PlayerLoaded = true

ApplyMetadata(ESX.PlayerData.metadata)

local timer = GetGameTimer()
while not HaveAllStreamingRequestsCompleted(ESX.PlayerData.ped) and (GetGameTimer() - timer) < 2000 do
Wait(0)
Expand All @@ -58,8 +46,10 @@ RegisterNetEvent("esx:playerLoaded", function(xPlayer, _, skin)
StartServerSyncLoops()
end)

local isFirstSpawn = true
ESX.SecureNetEvent("esx:onPlayerLogout", function()
ESX.PlayerLoaded = false
isFirstSpawn = true
end)

ESX.SecureNetEvent("esx:setMaxWeight", function(newMaxWeight)
Expand All @@ -72,7 +62,21 @@ local function onPlayerSpawn()
end

AddEventHandler("playerSpawned", onPlayerSpawn)
AddEventHandler("esx:onPlayerSpawn", onPlayerSpawn)
AddEventHandler("esx:onPlayerSpawn", function()
onPlayerSpawn()

if isFirstSpawn then
isFirstSpawn = false

if ESX.PlayerData.metadata.health and (ESX.PlayerData.metadata.health > 0 or Config.SaveDeathStatus) then
SetEntityHealth(ESX.PlayerData.ped, ESX.PlayerData.metadata.health)
end

if ESX.PlayerData.metadata.armor and ESX.PlayerData.metadata.armor > 0 then
SetPedArmour(ESX.PlayerData.ped, ESX.PlayerData.metadata.armor)
end
end
end)

AddEventHandler("esx:onPlayerDeath", function()
ESX.SetPlayerData("ped", PlayerPedId())
Expand Down
1 change: 1 addition & 0 deletions [core]/es_extended/shared/config/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Config.LogPaycheck = false -- Logs paychecks to a nominated Discord channel via
Config.EnableSocietyPayouts = false -- pay from the society account that the player is employed at? Requirement: esx_society
Config.MaxWeight = 24 -- the max inventory weight without a backpack
Config.PaycheckInterval = 7 * 60000 -- how often to receive paychecks in milliseconds
Config.SaveDeathStatus = true -- Save the death status of a player
Config.EnableDebug = false -- Use Debug options?

Config.DefaultJobDuty = true -- A players default duty status when changing jobs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ function Multicharacter:PlayerLoaded(playerData, isNew, skin)

TriggerServerEvent("esx:onPlayerSpawn")
TriggerEvent("esx:onPlayerSpawn")
TriggerEvent("playerSpawned")
TriggerEvent("esx:restoreLoadout")

self:Reset()
Expand Down

0 comments on commit 2bfd7ef

Please sign in to comment.