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 lastHea
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
player.Character.Humanoid.HealthChanged:connect(function(value)
if value ~= lastHea then
local damageTaken = value - lastHea
print(damageTaken)
end
end)
repeat
wait(.1)
lastHea = player.Humanoid.Health
until false
Simple math, you can subtract current health from the maximum health, that’s how much damage humanoid got.
Example:
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
local CurrentHealth = Humanoid.Health
local MaxHealth = Humanoid.MaxHealth
local Damage = MaxHealth-CurrentHealth
print(Damage) --prints out how much health humanoid lost.
end)
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.
Example:
PreviousHealth = Humanoid.Health
local function onChanged(property)
Difference = PreviousHealth - Humanoid.Health
--do whatever you want
end
-- Connect the event
Humanoid:GetPropertyChangedSignal("Health"):Connect(onChanged)
You should use HealthChanged as it was made for a reason.
This event fires when the Humanoid.Health changes. However, it will not fire if the health is increasing from a value equal to or greater than the Humanoid.MaxHealth.
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.