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