Fix health metadata not saving on reconnect#1214
Closed
AndyBodnar wants to merge 2 commits into
Closed
Conversation
- 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
|
Does this also fix the issue of hunger and thirst metadata not saving on reconnect? |
|
This PR has had 60 days of inactivity & will close within 7 days |
There was a problem hiding this comment.
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.healthdefault (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 on lines
+171
to
+176
| if health then | ||
| Player.Functions.SetMetaData('health', health) | ||
| end | ||
| if armor then | ||
| Player.Functions.SetMetaData('armor', armor) | ||
| end |
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 on lines
+14
to
+15
| if health and health > 0 then | ||
| SetEntityHealth(ped, health) |
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() |
Member
|
Adding onto my PR here #1221 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
healthfield existed in the default metadataSolution
config.lua
health = 200to default player metadataclient/loops.lua
QBCore:UpdatePlayerto send current health and armor to serverserver/events.lua
QBCore:UpdatePlayerhandler to save received health/armor to metadataplayerDroppedhandler to capture health/armor before saving on disconnectclient/events.lua
OnPlayerLoadedeventTesting
Tested using LuaGround - all metadata persistence tests pass.
Related Issue
Fixes #1213