diff --git a/client/events.lua b/client/events.lua index c065d7c7a..49fb272be 100644 --- a/client/events.lua +++ b/client/events.lua @@ -4,6 +4,21 @@ RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() ShutdownLoadingScreenNui() LocalPlayer.state:set('isLoggedIn', true, false) + + -- Restore saved health and armor after ped is ready + CreateThread(function() + Wait(1000) -- Wait for ped to be fully spawned + local ped = PlayerPedId() + local health = QBCore.PlayerData.metadata['health'] + local armor = QBCore.PlayerData.metadata['armor'] + if health and health > 0 then + SetEntityHealth(ped, health) + end + if armor and armor > 0 then + SetPedArmour(ped, armor) + end + end) + if not QBCore.Config.Server.PVP then return end SetCanAttackFriendly(PlayerPedId(), true, false) NetworkSetFriendlyFireOption(true) diff --git a/client/loops.lua b/client/loops.lua index 669a00e76..8b5729b4c 100644 --- a/client/loops.lua +++ b/client/loops.lua @@ -3,7 +3,10 @@ CreateThread(function() local sleep = 1000 if LocalPlayer.state.isLoggedIn then sleep = (1000 * 60) * QBCore.Config.UpdateInterval - TriggerServerEvent('QBCore:UpdatePlayer') + local ped = PlayerPedId() + local health = GetEntityHealth(ped) + local armor = GetPedArmour(ped) + TriggerServerEvent('QBCore:UpdatePlayer', health, armor) end Wait(sleep) end diff --git a/config.lua b/config.lua index 3c773097b..b98bfce58 100644 --- a/config.lua +++ b/config.lua @@ -67,6 +67,7 @@ QBConfig.Player.PlayerDefaults = { isdead = false, inlaststand = false, armor = 0, + health = 200, ishandcuffed = false, tracker = false, injail = 0, diff --git a/server/events.lua b/server/events.lua index bd45ec6b4..c451c459e 100644 --- a/server/events.lua +++ b/server/events.lua @@ -11,6 +11,11 @@ AddEventHandler('playerDropped', function(reason) local src = source if not QBCore.Players[src] then return end local Player = QBCore.Players[src] + local ped = GetPlayerPed(src) + if ped and DoesEntityExist(ped) then + Player.Functions.SetMetaData('health', GetEntityHealth(ped)) + Player.Functions.SetMetaData('armor', GetPedArmour(ped)) + end TriggerEvent('qb-log:server:CreateLog', 'joinleave', 'Dropped', 'red', '**' .. GetPlayerName(src) .. '** (' .. Player.PlayerData.license .. ') left..' .. '\n **Reason:** ' .. reason) TriggerEvent('QBCore:Server:PlayerDropped', Player) Player.Functions.Save() @@ -149,7 +154,7 @@ end) -- Player -RegisterNetEvent('QBCore:UpdatePlayer', function() +RegisterNetEvent('QBCore:UpdatePlayer', function(health, armor) local src = source local Player = QBCore.Functions.GetPlayer(src) if not Player then return end @@ -163,6 +168,12 @@ RegisterNetEvent('QBCore:UpdatePlayer', function() end Player.Functions.SetMetaData('thirst', newThirst) Player.Functions.SetMetaData('hunger', newHunger) + if health then + Player.Functions.SetMetaData('health', health) + end + if armor then + Player.Functions.SetMetaData('armor', armor) + end TriggerClientEvent('hud:client:UpdateNeeds', src, newHunger, newThirst) Player.Functions.Save() end)