New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How do I reference "Player HP"?

Asked by
net_h 11
5 days ago

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")

0
have you even considered looking at the wiki BashCaster 2554 — 4d
0
no because i'm a brainlet net_h 11 — 4d

2 answers

Log in to vote
0
Answered by
royaltoe 3678 Moderation Voter Community Moderator
4 days ago

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)
Ad
Log in to vote
0
Answered by 4 days ago
Edited 4 days ago
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)

Answer this question