HealthChanged sometimes fires twice even when humanoid is dead

(apologies if the name doesnt fit perfectly)

Currently working on a system to detect damage, For detecting death, It originally checked the Humanoid.Died event, only for me to release how delayed it is.
Instead, i just check if the health is 0 via the HealthChanged event.
(ignore how it doesnt have any checks, this is just simple code to represent what i have)

humanoid.HealthChanged:Connect(function()
	if humanoid.Health > 0 then 
		print("Damaged.")
	end
	if humanoid.Health <= 0 then
		print("Died.")
	end

end)

It works well, its not delayed.
But, one time when i was testing, it did the died check thing multiple times (it didnt print, i dont have prints, its just example code)

Any help is appreciated.

1 Like

Does your character have a Health script inside? If it heal continuously, it will print each time. Or maybe you mean it print twice when dead?

1 Like

As i said, the script provided is only to represent what my code is like, there are already checks to see if the player is damaged or was healed.

Like the title says, It sometimes fires twice when the humanoid dies.

to fix it, you could add something like a value with a name like “dead” and if the value is here then return

It might print multiple times if the health changes rapidly in a single frame.

You could try a Boolean variable to check if it’s already been printed for example;

local hasDied = false

humanoid.HealthChanged:Connect(function()
    if humanoid.Health > 0 then 
        print("Damaged.")
    end
    if humanoid.Health <= 0 and not hasDied then
        print("Died.")
        hasDied = true
    end
end)
1 Like

Alright.
i will try yours and @Boustifail 's answers (they are basically the same but i didnt want to exclude anyone)
I will test it out in a bit

Maybe the variable would be better since it doesnt create something

I forgot to mention (mostly just forgot in general lol)

the script detects this for every player (For like a damage indicator)
So using a variable would make it work only once, Would using “Return” possibly work?

Then maybe use my method and instead of the name, use the Player.UserId. If the UserId is there then do not fire

try using lastHealth to avoid incorrect calculations:

local hasDied, lastHealth = false, humanoid.Health

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if humanoid.Health > 0 then
		if humanoid.Health < lastHealth then
			print("Damaged.")
		end
	else
		hasDied = true
		
		print("Died.")
	end
	
	lastHealth = humanoid.Health
end)
1 Like

The “Damaged.” print is not the problem of this post. As i said in a different reply, I already have code that detect if they were damaged. Plus, as i stated aswell, using a variable will not work since i also said that it does this for every player in one script.

In this case, the mistake is that health is regenerated too quickly; as far as I know, by default health is restored quickly, but not in large steps.

No, I don’t believe thats the issue, Since im also testing this code at an NPC, which doesn’t have auto regen, or any scripts for that matter.
And i don’t think that makes sense with my problem either, since im pretty sure Roblox doesnt force a humanoid’s health to go to 0 everytime they regen health.

Try running the game through a real server, and not through Roblox Studio, and check the result, this may be the case, since playing the game in Studio and on a real server is different.

I finally figured out what happened, But now i have a different problem…

Everytime you damage an already dead NPC/Player, the “HealthChanged” event still fires.
What i was doing, is that i hit the NPC twice, but really quickly, So now i dont know what i should do…