Skip to content

Fix health metadata not saving on reconnect#1214

Closed
AndyBodnar wants to merge 2 commits into
qbcore-framework:mainfrom
AndyBodnar:fix/health-metadata-save
Closed

Fix health metadata not saving on reconnect#1214
AndyBodnar wants to merge 2 commits into
qbcore-framework:mainfrom
AndyBodnar:fix/health-metadata-save

Conversation

@AndyBodnar
Copy link
Copy Markdown

Summary

Fixes player health resetting to full on reconnect by properly saving and restoring health metadata.

Problem

When players disconnect and reconnect, their health resets to full instead of being preserved. This happens because:

  1. No health field existed in the default metadata
  2. Health was never captured from the client
  3. Health was never saved to the database
  4. Health was never restored on spawn

Solution

config.lua

  • Added health = 200 to default player metadata

client/loops.lua

  • Modified QBCore:UpdatePlayer to send current health and armor to server

server/events.lua

  • Modified QBCore:UpdatePlayer handler to save received health/armor to metadata
  • Modified playerDropped handler to capture health/armor before saving on disconnect

client/events.lua

  • Added health/armor restoration in OnPlayerLoaded event

Testing

Tested using LuaGround - all metadata persistence tests pass.

Related Issue

Fixes #1213

- 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 qbcore-framework#1213
@06875878
Copy link
Copy Markdown

06875878 commented Mar 11, 2026

Does this also fix the issue of hunger and thirst metadata not saving on reconnect?

@github-actions
Copy link
Copy Markdown

This PR has had 60 days of inactivity & will close within 7 days

@github-actions github-actions Bot added the Stale label May 10, 2026
@GhzGarage GhzGarage added the enhancement New feature or request label May 13, 2026
@GhzGarage GhzGarage requested a review from Copilot May 13, 2026 04:55
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #1213 by persisting player health across reconnects via QBCore player metadata, so reconnecting no longer resets players to full health.

Changes:

  • Add metadata.health default (200) so all players have a persisted health field.
  • Send health/armor from client periodic updates and persist them server-side (including on disconnect).
  • Restore saved health/armor on QBCore:Client:OnPlayerLoaded.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
config.lua Adds health = 200 to default player metadata so the field exists for all players.
client/loops.lua Includes current ped health/armor in the periodic QBCore:UpdatePlayer server update.
server/events.lua Persists received health/armor into metadata on update; captures health/armor in playerDropped before saving.
client/events.lua Restores saved health/armor on OnPlayerLoaded after a short delay.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/events.lua
Comment on lines +171 to +176
if health then
Player.Functions.SetMetaData('health', health)
end
if armor then
Player.Functions.SetMetaData('armor', armor)
end
Comment thread server/events.lua
Comment on lines +157 to 160
RegisterNetEvent('QBCore:UpdatePlayer', function(health, armor)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
Comment thread client/events.lua
Comment on lines +14 to +15
if health and health > 0 then
SetEntityHealth(ped, health)
Comment thread client/events.lua
Comment on lines 4 to +11
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()
@github-actions github-actions Bot removed the Stale label May 13, 2026
@GhzGarage
Copy link
Copy Markdown
Member

Adding onto my PR here #1221

@GhzGarage GhzGarage closed this May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] health metadata not update

4 participants