Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How could I detect a player's health?

Could I use this?

p = game.Players.LocalPlayer.Humanoid.Health
if p= 10 then
print("Player is a bout to die!")

I always had trouble with LocalPlayers and stuff... so can somebody help me? Thanks!

3 answers

✓ Accepted answer

This is probably what you're looking for. The way it was done before was not only wrong even if it was correct it would have only ran once therefore never printing unless you disabled and enabled the script exactly when the health is 10(near impossible).

local plr = game.Players.LocalPlayer
if not plr.Character then plr.CharacterAdded:wait() end
local char = plr.Character
local hum = char:WaitForChild("Humanoid")

hum.HealthChanged:connect(function(health)
	if health <= 10 then
		print("Player is about to die!")
	end
end)

Note: This should be a local script in StarterPack.

LocalPlayer

Humanoid

HealthChanged

2 comments

0

What I'm trying to do is that if a player has 10-50 health left, a gui pops up and says you are about to die. I know how to do the GUI part, but I need a health detection first.

0

Woops, sorry! I didnt see your last answer. thanks!

Pretty much. Just some minor errors, and you're good. Try this:

p = game.Players.LocalPlayer.Humanoid.Health
if p== 10 then
print("Player is a bout to die!")
end

Players.LocalPlayer is a local field, so you need to do this from a LocalScript, not a regular Script.

2 comments

0

LocalPlayer is not a function at all. It is a property of the PlayerService returning an object with the current player if mentioned in a local script

0

Yes, it's a field. Sorry about that.

There are several ways to do this, but you always use the Health property of the Humanoid that is inside the character of a player.

In the example you gave, you forgot to go into the Character and instead you're looking for a Humanoid inside the Player, which is not there. This is the improved script:

p = game.Players.LocalPlayer.Character.Humanoid.Health
if p == 10 then -- always use the == operator in comparisons. Remember: == means "equals to", = means "set to"
	print("Player is about to die!")
end -- also, don't forget to end your if statement!

This discussion is preserved as it was. New answers, comments, and votes are closed.