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

Why does the health not reset?

Asked by 8 years ago

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

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

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)
0
Um, dude, I tested this out again, the same thing happened. NeonicPlasma 181 — 8y
0
Explain, por favor. It worked when I tested it. Although on second though we should use HealthChanged instead of Changed, just for efficiency. Perci1 4988 — 8y
Ad

Answer this question