Why doesn't this work? Checking if Humanoid.Health <= 0

Hi, here’s the code chunk

remoteFolder.damageinflicted.OnServerEvent:Connect(function(inflicter,reciever,amount)--inflicter = player instance, reciever = character model
	reciever.Humanoid.Health -= amount
	if reciever.Humanoid.Health <= 0 then --this doesn't work?
		inflicter.leaderstats.elims.Value += 1
	end
end)

The remote event, parameters, and health deduction all work perfectly. However, for some reason the if statement doesn’t fire whenever the reciever’s health equals or goes below 0 from the previous line

If I could get some help with this that’d be great ;]

1 Like

If you want to detect if the player died you should just use a separate RBXScriptSignal
I mean there’s one specifically for that purpose
https://developer.roblox.com/en-us/api-reference/event/Humanoid/Died

1 Like

I know about that, but because of the circumstances this is being used in, it wouldn’t be good. I’m asking why the if statement doesn’t work

1 Like

Why is that? Do note that I suggested an alternative because of the XY problem

Are you sure the damage infliction works properly? Have you tried printing it out in the output to check?

Also:
image

2 Likes

I suppose I should have made it clearer I’m not looking for a fix to “X”, but rather a reason why “Y” doesn’t work.

Yes the damage works well, I tested with 2 players

Didn’t know about TakeDamage, thank you

The only reason why the if statement won’t run is because the logic is false. And the only reason why the logic would be false is because the player’s health isn’t actually at or below 0, indicating that the player was never killed, which is why I draw the conclusion that your method of inflicting damage is NOT GOOD

Or there’s some kind of replication issue


^You can see both clients and the server see the damage. No replication issues…

I don’t know why you don’t believe me when I say the damage infliction works. Stop being a jerk about it too. The logic isn’t “false”. And it’s not “my method”, I’m curious as to why Humanoid.Health <= 0 doesn’t work

I’m not being a jerk, I’m literally explaining to you why the if statement fundamentally doesn’t work, which IS because the logic is false, BECAUSE the player’s health literally isn’t at or below 0

What I’m saying is that the if statement should and only should run when the player dies. Yes, in that screenshot, that other player took damage but he didn’t die! So can you check to see what happens when you kill him?

can you try printing Humanoid.Health <= 0 tell me what that prints?

1 Like


^Player dead


^Script


^Server output is empty

1 Like

If you want to do a leaderboard kill system, You could instead tag the receivers humanoid for (x) seconds then a seperate script in the receiver checks if his humanoid has died with a tag

2 Likes

That’s a really good idea, I’ll probably do that. But I’m still confused why Humanoid.Healh <= 0 doesn’t work

2 Likes

printing the humanoid’s health does print what anyone would expect. that part works

print(reciever.Humanoid.Health <= 0)

Try this when you run it. tell me if it true or false

1 Like

Then, I’m gonna be honest with you, I have no idea what went wrong. I told you the exact reason as to why the if statement wouldn’t work, and that’s all I have to say

Do you mind sharing the place file with us so we can test it too?

1 Like

it prints true‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎ when the player has died, but only if it is outside the if statement

1 Like

Then try storing the bool as a local and use that in the if statement instead

local a = reciever.Humanoid.Health <= 0
if a then

end
1 Like

I made a copy of the place and Humanoid.Health <= 0 worked in it… restarted the non-copy and now it works too
i probably should’ve thought about restarting studio first…

1 Like

https://developer.roblox.com/en-us/api-reference/event/Humanoid/Died

Consider listening for the “.Died” event to fire on that “Humanoid” instance instead as it’s the correct/proper way to go about waiting/detecting a humanoid’s death.