How can i check if the humanoid's health is 0?

I’m writing a script that checks if the localplayer’s health is 0. However, ive tried many different codes and they just don’t seem to work. THANKS!

It also is supposed to fire a RemoteEvent which runs another code.

Heres my localscript:

local players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local localPlayer = players.LocalPlayer
local Humanoidevent = rs:WaitForChild("Humanoid")

local humanoid = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
	print("Humanoid found")
	if humanoid.Health == 0 then
		print("Player's health is 0")
		Humanoidevent:FireServer(localPlayer)
	end
end

(Yes “Humanoid found” prints.)

the reason this doesn’t work is because the script goes thru that health check once (as soon as the script loads) so when it checks, the humanoid will still be at 100 health.

You should instead just use Humanoid.Died to check

local players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local localPlayer = players.LocalPlayer
local Humanoidevent = rs:WaitForChild("Humanoid")

local humanoid = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Humanoid")
humanoid.Died:Connect(function()
    print("Player's health is 0")
	Humanoidevent:FireServer(localPlayer)
end)
1 Like

Use signals such as Humanoid.Died
If you want to track health change then use Humanoid:GetPropertyChangedSignal(“Health”):Connect(OnHealthChanged)

2 Likes

If that humanoid has Enum.HumanoidStateType.Dead state enabled, then you can you the Died event. Otherwise you should manually check for the health value either in a loop with a reasonable delay, or within the RunService events callback.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.