From 07528dea83700a32450c873e8cd357017d7a9a11 Mon Sep 17 00:00:00 2001 From: Solareon <769465+solareon@users.noreply.github.com> Date: Wed, 25 Sep 2024 22:28:45 +0200 Subject: [PATCH] fix(server/events): remove threads from handler (#583) --- server/events.lua | 10 ++++------ server/player.lua | 31 +++++++++++++++++-------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/server/events.lua b/server/events.lua index d79683b4d..a8b4fc277 100644 --- a/server/events.lua +++ b/server/events.lua @@ -204,12 +204,10 @@ local function playerStateBagCheck(bagName, meta, value) if not value then return end local plySrc = GetPlayerFromStateBagName(bagName) if not plySrc then return end - CreateThread(function() - local player = QBX.Players[plySrc] - if not player then return end - if player.PlayerData.metadata[meta] == value and Player(plySrc).state[meta] == value then return end - player.Functions.SetMetaData(meta, value) - end) + local player = QBX.Players[plySrc] + if not player then return end + if player.PlayerData.metadata[meta] == value then return end + player.Functions.SetMetaData(meta, value) end ---@diagnostic disable-next-line: param-type-mismatch diff --git a/server/player.lua b/server/player.lua index 86840c2d7..df2398cd0 100644 --- a/server/player.lua +++ b/server/player.lua @@ -669,26 +669,29 @@ function CreatePlayer(playerData, Offline) function self.Functions.SetMetaData(meta, val) if not meta or type(meta) ~= 'string' then return end - if (meta == 'hunger' or meta == 'thirst' or meta == 'stress') then - val = lib.math.clamp(val, 0, 100) - if not self.Offline then - Player(self.PlayerData.source).state:set(meta, val, true) - end - end - - if (meta == 'dead' or meta == 'inlaststand') and not self.Offline then - Player(self.PlayerData.source).state:set('canUseWeapons', not val, true) - end - local oldVal = self.PlayerData.metadata[meta] self.PlayerData.metadata[meta] = val self.Functions.UpdatePlayerData() - if meta == 'inlaststand' or meta == 'isdead' then - self.Functions.Save() - end + if not self.Offline then + local playerState = Player(self.PlayerData.source).state TriggerClientEvent('qbx_core:client:onSetMetaData', self.PlayerData.source, meta, oldVal, val) TriggerEvent('qbx_core:server:onSetMetaData', meta, oldVal, val, self.PlayerData.source) + + if (meta == 'hunger' or meta == 'thirst' or meta == 'stress') then + val = lib.math.clamp(val, 0, 100) + if playerState[meta] ~= val then + playerState:set(meta, val, true) + end + end + + if (meta == 'dead' or meta == 'inlaststand') then + playerState:set('canUseWeapons', not val, true) + end + end + + if meta == 'inlaststand' or meta == 'isdead' then + self.Functions.Save() end end