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)
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)
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?
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)
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.
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…