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

How do I reference "Player HP"?

for example if I wanted a script to print "Damaged" when the player goes below 50 hp. How do I do that? for example if (?????) <50 print("Damaged")

2 comments

0

have you even considered looking at the wiki

0

no because i'm a brainlet

2 answers

You can use humanoid's HealthChanged event.

Using a server script:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)		
		character:WaitForChild("Humanoid").HealthChanged:Connect(function()
			print("player's health changed")
		end)
	end)
end)

Using a local script:

local player = game.Players.LocalPlayer 
local character = player.Character or player.CharacterAdded:Wait()

character:WaitForChild("Humanoid").HealthChanged:Connect(function()
	print("player's health changed")
end)
game.Players.PlayerAdded:Connect(function(Player)
repeat wait() until Player.CharacterAdded and Player.Character ~= nil
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Health = Humanoid.Health -- now you can change health, everything i did before ensures the character exists.
-- what happens when he's damaged.
Humanoid.HealthChanged:Connect(function()
print("Humanoid health configured to "..tostring(Health))
end)
end)

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