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

What can I define as a player's health?

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

✓ Accepted answer

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)

3 comments

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?

0

For sure. Edit your post and put your new code in

0

I figured it out, thanks for the help.

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