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