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

What can I define as a player's health?

Asked by
Uzuntu 6
6 years ago

I'm working on a script that detects when a player's health has reached 0. Any way to do this? This is what I tried.

local player = game.Players:GetPlayers()

and

elseif player.Character.Humanoid.Health == 0 then

    print (player.Name)" has died!"

end

Any help is appreciated.

1 answer

Log in to vote
0
Answered by
Thetacah 712 Moderation Voter
6 years ago

Use the .Died event! It is fired after a character has entered the dead state, usually when their health reaches 0. Nonetheless, your player variable is a table of players, which wouldn't work!

Just got the following code from this wiki article.

You'll want to put this in a script in ServerScriptStorage. Since we're using a ServerSide script, it can at times be difficult to define the player....As such, a quick and efficient way to accomplish such a task would be to use both the playeradded event and the characteradded event to then be able to link to the players humanoid (The .Died event fires off of the humanoid) .


game:GetService('Players').PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() print(player.Name .. " has died!") end) end) end)
0
I tried simplifying this so it'll work in an ifelse statement, but it told me 'Character' is a nil value. Should I post what I did? Uzuntu 6 — 6y
0
For sure. Edit your post and put your new code in Thetacah 712 — 6y
0
I figured it out, thanks for the help. Uzuntu 6 — 6y
Ad

Answer this question