Checking if yourh health is at 0

Hello, So I am trying to find out how to tell when the players health is at 0 so it kicks them this is my code so far

if game.Players.LocalPlayer.Character.Humanoid.Health = 0 then
	print("hi")
end

but it doesnt seem to work, how is a way to tell if its at 0 this is in a local script by the way and also the print hi was just for testing to see if it even worked, any info on how to do this would be appreciated.

2 Likes

==, not =.

Yep I am so dumb, sorry to waste ur time your now marked as a solution.

You need to add two equal signs not one if you are using an if statement. And if your health can’t go below 0 I suggest using:

if game.Players.LocalPlayer.Character.Humanoid.Health < 0.1 then
	print("hi")
end

anything that is not “equal to” in and if statement can just be one letter instead of two.

Wait, while your here it still doesnt work but for another error it says

attempt to index nil with 'Humanoid' 

you might want to use this script:

local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

Before the script so that it waits until the humanoid exists or is found. Your script gives an error because scripts (especially server scripts) run as fast as they can once they are loaded.

Now it says

attempt to index nil with 'WaitForChild'

player.Character can be nil when a game starts. The solution depends on what you’re using this script for.

Does it depend where I place the script where do you suggest I place it?

No, it depends on what you want the script to do.

Are you just trying to run something whenever a person dies?

if it is a server script then yes. I would say put the script in StarterPlayer > StarterCharacterScripts so the script will load with the player’s character so you don’t need to make it too complicated.

Currently just trying to print hi. heres the script if you wanna take a look I prob did something wrong

local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
if Humanoid.Health == 0 then
	print("hi")
end

But do you want to print hi whenever someone dies? Because that’s a different question to “checking if a humanoid’s health equals 0”

print hi whenever somebody dies, so I must be doing it wrong, can you tell me how to tell if somebody dies

This is how you check when someone dies.

1 Like

Thank you, I will look into it.

I mean there’s not pretty much to look there, theres basically a code sample which you can copy.

I try to learn the code and not copy it, so I understand it in the future.

1 Like

Also you should first search on the internet your question maybe someone already asked it and has an answer.

1 Like

I did, and found nothing on my side that helped me understand my problem.