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

How to I detect when a players health is zero?

Asked by 4 years ago

I'm trying to make a part of my game where when you die 3 times, you respawn at the start of the first level again. The only issue is, the Humaniod.Dead wont work, I can't seem to detect when the player dies, or when their health is zero. I have tried multiple sources, and this is all I can seem to come up with.

if Character.Humanoid.Health.Value <= 0 then -- if the players health is less than 0... print("Checking for health") workspace.Lives.Value = workspace.Lives.Value - 1 --Changes the lives left script.Parent.Text = "Lives: " .. workspace.Lives.Value --Changes the GUI to display accurate health status end I am relatively new to scripting in Roblox, so please ignore the poor scripting, and any obvious errors.

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

do this, but substitute your script for the Print() and make sure its a local script:

local plr = game.Players.LocalPlayer
local h = plr.Character:WaitForChild("Humanoid")

h.Changed:connect(function(s)
if h.Health <= 0 then
    print("Dead Boi")
    end
end)

Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago

You can use the HealthChanged event of Humanoid to actively match the health when it appears to change, use the <= operators to identify if the Health is less than or equal to zero.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.HealthChanged:Connect(function(HeathState)
    if (HealthState <= 0) then
        --// Code
    end
end)

You can optionally use the Died RBXScriptSignal to connect an identical listener.

Humanoid.Died:Connect(function()
    --// Code
end))

Answer this question