Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does the health not reset?

So I made a health display using math.floor. The only problem is, every time I reset, it still shows 0 when I respawn. Why?

local player = game:GetService("Players").LocalPlayer
local h = player.Character.Humanoid
h.Changed:connect(function()
	script.Parent.Text = math.floor(h.Health)
end)

If you could help, I'd appreciate that.

1 answer

✓ Accepted answer

Online, the Character and the Humanoid probably won't exist when the script runs, so we should definitely wait for those.

repeat wait() until player.Character
local h = player.Character:WaitForChild("Humanoid")

Anyways, your main problem is that the Changed event won't be firing when you respawn. This is because the Humanoid is already at full health by the time it's connected to the event, so no change is detected. This can easily be fixed in a single line:

local player = game:GetService("Players").LocalPlayer
	repeat wait() until player.Character
local h = player.Character:WaitForChild("Humanoid")

script.Parent.Text = math.floor(h.Health) --Now the text will be set to the Health once, no matter what, when the code first runs.

h.HealthChanged:connect(function()--And every time it changes.
	script.Parent.Text = math.floor(h.Health)
end)

2 comments

0

Um, dude, I tested this out again, the same thing happened.

0

Explain, por favor. It worked when I tested it. Although on second though we should use HealthChanged instead of Changed, just for efficiency.

This discussion is preserved as it was. New answers, comments, and votes are closed.