How to check amount of damage taken

  1. I want to check the amount of health my humanoid loses
  2. Works fine but a lot of times the change happens more than once in 1 tenth of a second
  3. 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

Thanks for taking your time to help

2 Likes

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)
3 Likes

i know but i want to subtrack my current health from my new current health because i wont always have max health

for example if my max health is 100 and my current is 70. it gets changed into 20 i want a way to calculate 50

:GetPropertyChangedSignal(“Health”) happens everytimes Humanoid’s health changes.

4 Likes

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)
4 Likes

ohhh thanks that works lol. I just tried it

2 Likes

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.

1 Like

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.