How would i run a function when only when the health is damaged

Hi, i wanna run a function when the humanoids health has decressed.

Normaly its this

Humanoid.HealthChanged
That decects if the health is increased and decreased.
But i only want i to check if its decreased.

1 Like

You can still utilize HealthChanged.
Just check if newHealth is lower than oldHealth.

Humanoid.HealthChanged:Connect(function(oldHealth, newHealth)
    if newHealth < oldHealth then

    end
end)
1 Like

Getting the error -

attempt to compare nil and number

you need to define oldHealth…

It is defined. Look Humanoid.HealthChanged:Connect(function(oldHealth, newHealth)

oldHealth is not a valid arguement of Humanoid.HealthChanged the first arguement of the function is the newhealth. you need to see if the newhealth is less than the original health. i honestly dont think that figuring out what the humanoids current health is and comparing it to the new health is very hard for even a 10 year old.

1 Like

Oh, my bad. The only parameter is newHealth.
There’s a snippet on the wiki that can help you achieve what you’re doing.
https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged


You could then check if health is less than currentHealth.

3 Likes