Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

How could I detect a player's health?

Asked by 10 years ago

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

Log in to vote
1
Answered by
Lacryma 548 Moderation Voter
10 years ago

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

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. PyccknnXakep 1225 — 10y
0
Woops, sorry! I didnt see your last answer. thanks! PyccknnXakep 1225 — 10y
Ad
Log in to vote
1
Answered by 10 years ago

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.

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 juzzbyXfalcon 155 — 10y
0
Yes, it's a field. Sorry about that. iconmaster 301 — 10y
Log in to vote
0
Answered by 10 years ago

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!

Answer this question