Skip to content

Commit e87379a

Browse files
committed
Fix health metadata not saving on reconnect
- Add `health` field to default player metadata (config.lua) - Send health/armor from client to server in UpdatePlayer event (loops.lua) - Save health/armor to metadata in QBCore:UpdatePlayer handler (events.lua) - Capture health/armor on disconnect before save (events.lua) - Restore saved health/armor on player load (client/events.lua) This ensures player health persists across reconnects instead of resetting to full. Fixes #1213
1 parent 36d8986 commit e87379a

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

client/events.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
55
ShutdownLoadingScreenNui()
66
LocalPlayer.state:set('isLoggedIn', true, false)
7+
8+
-- Restore saved health and armor after ped is ready
9+
CreateThread(function()
10+
Wait(1000) -- Wait for ped to be fully spawned
11+
local ped = PlayerPedId()
12+
local health = QBCore.PlayerData.metadata['health']
13+
local armor = QBCore.PlayerData.metadata['armor']
14+
if health and health > 0 then
15+
SetEntityHealth(ped, health)
16+
end
17+
if armor and armor > 0 then
18+
SetPedArmour(ped, armor)
19+
end
20+
end)
21+
722
if not QBCore.Config.Server.PVP then return end
823
SetCanAttackFriendly(PlayerPedId(), true, false)
924
NetworkSetFriendlyFireOption(true)

client/loops.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ CreateThread(function()
33
local sleep = 0
44
if LocalPlayer.state.isLoggedIn then
55
sleep = (1000 * 60) * QBCore.Config.UpdateInterval
6-
TriggerServerEvent('QBCore:UpdatePlayer')
6+
local ped = PlayerPedId()
7+
local health = GetEntityHealth(ped)
8+
local armor = GetPedArmour(ped)
9+
TriggerServerEvent('QBCore:UpdatePlayer', health, armor)
710
end
811
Wait(sleep)
912
end

config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ QBConfig.Player.PlayerDefaults = {
6767
isdead = false,
6868
inlaststand = false,
6969
armor = 0,
70+
health = 200,
7071
ishandcuffed = false,
7172
tracker = false,
7273
injail = 0,

server/events.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ AddEventHandler('playerDropped', function(reason)
1111
local src = source
1212
if not QBCore.Players[src] then return end
1313
local Player = QBCore.Players[src]
14+
local ped = GetPlayerPed(src)
15+
if ped and DoesEntityExist(ped) then
16+
Player.Functions.SetMetaData('health', GetEntityHealth(ped))
17+
Player.Functions.SetMetaData('armor', GetPedArmour(ped))
18+
end
1419
TriggerEvent('qb-log:server:CreateLog', 'joinleave', 'Dropped', 'red', '**' .. GetPlayerName(src) .. '** (' .. Player.PlayerData.license .. ') left..' .. '\n **Reason:** ' .. reason)
1520
TriggerEvent('QBCore:Server:PlayerDropped', Player)
1621
Player.Functions.Save()
@@ -149,7 +154,7 @@ end)
149154

150155
-- Player
151156

152-
RegisterNetEvent('QBCore:UpdatePlayer', function()
157+
RegisterNetEvent('QBCore:UpdatePlayer', function(health, armor)
153158
local src = source
154159
local Player = QBCore.Functions.GetPlayer(src)
155160
if not Player then return end
@@ -163,6 +168,12 @@ RegisterNetEvent('QBCore:UpdatePlayer', function()
163168
end
164169
Player.Functions.SetMetaData('thirst', newThirst)
165170
Player.Functions.SetMetaData('hunger', newHunger)
171+
if health then
172+
Player.Functions.SetMetaData('health', health)
173+
end
174+
if armor then
175+
Player.Functions.SetMetaData('armor', armor)
176+
end
166177
TriggerClientEvent('hud:client:UpdateNeeds', src, newHunger, newThirst)
167178
Player.Functions.Save()
168179
end)

0 commit comments

Comments
 (0)