From a8e372b9488e2abed84187091e35292c8bdbe7e9 Mon Sep 17 00:00:00 2001 From: Cocodrulo <142546774+Cocodrulo@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:31:42 +0000 Subject: [PATCH 1/4] feat: Add options to save player health and armor on respawn --- config.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config.lua b/config.lua index 3c773097b..5dc03ff46 100644 --- a/config.lua +++ b/config.lua @@ -13,6 +13,8 @@ QBConfig.Money.PayCheckTimeOut = 10 -- The time QBConfig.Money.PayCheckSociety = false -- If true paycheck will come from the society account that the player is employed at, requires qb-management QBConfig.Player = {} +QBConfig.Player.SaveHealth = true -- Save player health on respawn. +QBConfig.Player.SaveArmor = true -- Save player armor on respawn. QBConfig.Player.HungerRate = 4.2 -- Rate at which hunger goes down. QBConfig.Player.ThirstRate = 3.8 -- Rate at which thirst goes down. QBConfig.Player.Bloodtypes = { @@ -67,6 +69,7 @@ QBConfig.Player.PlayerDefaults = { isdead = false, inlaststand = false, armor = 0, + health = 0, ishandcuffed = false, tracker = false, injail = 0, From a63eeb95dbdc2c0374bbd849f5c86ff390e3900d Mon Sep 17 00:00:00 2001 From: Cocodrulo <142546774+Cocodrulo@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:31:48 +0000 Subject: [PATCH 2/4] feat: Implement player health and armor restoration on load --- client/events.lua | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/client/events.lua b/client/events.lua index 67755aa25..b36dc4a0d 100644 --- a/client/events.lua +++ b/client/events.lua @@ -3,10 +3,26 @@ -- if LocalPlayer.state['isLoggedIn'] then RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() ShutdownLoadingScreenNui() + local pid = PlayerPedId() LocalPlayer.state:set('isLoggedIn', true, false) if not QBCore.Config.Server.PVP then return end - SetCanAttackFriendly(PlayerPedId(), true, false) + SetCanAttackFriendly(pid, true, false) NetworkSetFriendlyFireOption(true) + + CreateThread(function () + Wait(4000) -- Wait for the player to load in properly + local player = PlayerId() + SetEntityMaxHealth(pid, 200) + SetPlayerHealthRechargeMultiplier(player, 0.0) + SetPlayerHealthRechargeLimit(player, 0.0) + if QBConfig.Player.SaveArmor then + SetPedArmour(pid, QBCore.PlayerData.metadata['armor']) + end + + if QBConfig.Player.SaveHealth then + SetEntityHealth(pid, QBCore.PlayerData.metadata['health']) + end + end) end) RegisterNetEvent('QBCore:Client:OnPlayerUnload', function() From 68f1950281593b17311cc5bf0828e314203e061d Mon Sep 17 00:00:00 2001 From: Cocodrulo <142546774+Cocodrulo@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:31:58 +0000 Subject: [PATCH 3/4] feat: Save player health and armor on player drop and update events --- server/events.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/server/events.lua b/server/events.lua index 00b0fa5a9..3f821a47e 100644 --- a/server/events.lua +++ b/server/events.lua @@ -13,6 +13,19 @@ AddEventHandler('playerDropped', function(reason) local Player = QBCore.Players[src] TriggerEvent('qb-log:server:CreateLog', 'joinleave', 'Dropped', 'red', '**' .. GetPlayerName(src) .. '** (' .. Player.PlayerData.license .. ') left..' .. '\n **Reason:** ' .. reason) TriggerEvent('QBCore:Server:PlayerDropped', Player) + + local ped = GetPlayerPed(src) + + if QBConfig.Player.SaveHealth and ped then + local health = GetEntityHealth(ped) + Player.Functions.SetMetaData('health', health) + end + + if QBConfig.Player.SaveArmor and ped then + local armour = GetPedArmour(ped) + Player.Functions.SetMetaData('armor', armour) + end + Player.Functions.Save() QBCore.Player_Buckets[Player.PlayerData.license] = nil QBCore.Players[src] = nil @@ -151,6 +164,7 @@ end) RegisterNetEvent('QBCore:UpdatePlayer', function() local src = source local Player = QBCore.Functions.GetPlayer(src) + local ped = GetPlayerPed(src) if not Player then return end local newHunger = Player.PlayerData.metadata['hunger'] - QBCore.Config.Player.HungerRate local newThirst = Player.PlayerData.metadata['thirst'] - QBCore.Config.Player.ThirstRate @@ -162,6 +176,17 @@ RegisterNetEvent('QBCore:UpdatePlayer', function() end Player.Functions.SetMetaData('thirst', newThirst) Player.Functions.SetMetaData('hunger', newHunger) + + if QBConfig.Player.SaveHealth and ped then + local health = GetEntityHealth(ped) + Player.Functions.SetMetaData('health', health) + end + + if QBConfig.Player.SaveArmor and ped then + local armour = GetPedArmour(ped) + Player.Functions.SetMetaData('armor', armour) + end + TriggerClientEvent('hud:client:UpdateNeeds', src, newHunger, newThirst) Player.Functions.Save() end) From ec8e2f8eb6bdf90c235cbd68c57fc25db174a476 Mon Sep 17 00:00:00 2001 From: Cocodrulo <142546774+Cocodrulo@users.noreply.github.com> Date: Sun, 12 Jan 2025 14:30:38 +0000 Subject: [PATCH 4/4] feat: Set default player health to 200 in configuration --- config.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.lua b/config.lua index 5dc03ff46..5320b9525 100644 --- a/config.lua +++ b/config.lua @@ -69,7 +69,7 @@ QBConfig.Player.PlayerDefaults = { isdead = false, inlaststand = false, armor = 0, - health = 0, + health = 200, ishandcuffed = false, tracker = false, injail = 0,