I want to check the amount of health my humanoid loses
Works fine but a lot of times the change happens more than once in 1 tenth of a second
Its a very barbaric solution and i couldn’t find an alternative to checking the amount of damage other than firing all clients the target and damage amount.
local player = game.Players.LocalPlayer
-- i need the "if value != lastHealth" because HealthChanged is affected MaxHealth change
-- its also a local script in my PlayerGui
if value ~= lastHea then
local damageTaken = value - lastHea
lastHea = player.Humanoid.Health
Simple math, you can subtract current health from the maximum health, that’s how much damage humanoid got.
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local CurrentHealth = Humanoid.Health
local MaxHealth = Humanoid.MaxHealth
local Damage = MaxHealth-CurrentHealth
print(Damage) --prints out how much health humanoid lost.
You can use :GetPropertyChangedSignal to detect when the Health value of the Humanoid changes. You can store the previous health to calculate the difference, as you are doing now. This will prevent you from having to constantly check it from a loop, and use up processing power.
PreviousHealth = Humanoid.Health
local function onChanged(property)
Difference = PreviousHealth - Humanoid.Health
--do whatever you want
-- Connect the event
Have you really checked if the event registers for every time the health changes? I don’t think the event is slower than GetPropertyChangedSignal approach and should be reliable.
Instead of recording the current health every single tenth of a second, only record a new last health variable when the HealthChanged function fires while keeping it by default (before HealthChanged ever fired) at the max health.
Because you use a loop iterating every 0.1 seconds, it won’t be able to set a new last health if changes were made faster than 0.1 seconds plus it is unnecessary when you can only check when it actually changes instead.