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

Is there a reason on why this health.Changed script won't work?

Asked by
oSyM8V3N 429 Moderation Voter
6 years ago
Edited 6 years ago
wait(game.Lighting.TimeToLoad.Value)

-- Health Bar
local HealthB = script.Parent.bk.HBar

repeat until game.Players.LocalPlayer.CharacterAdded:Wait()

game.Players.LocalPlayer.Character.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    local redzone = game.Players.LocalPlayer.Character.Humanoid.MaxHealth / 2
    if game.Players.LocalPlayer.Character.Humanoid.Health <= redzone then
        HealthB.ImageColor3 = Color3.new(255, 0, 0)
    end
    if game.Players.LocalPlayer.Character.Humanoid.Health > redzone then
        local brickcolor = BrickColor.new("Lime green")
        local color = brickcolor.Color
        HealthB.ImageColor3 = color
    end
    HealthB.Size = UDim2.new(game.Players.LocalPlayer.Character.Humanoid.Health / game.Players.LocalPlayer.Character.Humanoid.MaxHealth, 0, 0, 20)
end)

1 answer

Log in to vote
1
Answered by
lukeb50 631 Moderation Voter
6 years ago
Edited 6 years ago

You cannot add a Changed event to a property like that. It just won't work.

The way to do it is as follows:

game.Players.LocalPlayer.Character.Humanoid:GetPropertyChangedSignal("Health")
:Connect(function()

More info

repeat until game.Players.LocalPlayer.CharacterAdded:Wait()

repeat until are completely unnecessary and are also permanently running, preventing your event from connecting.

0
I was actually using a ImageLabel as the health bar, i updated the question. It still doesn't work even if i used the :GetPropertyChangedSigna; oSyM8V3N 429 — 6y
0
Just noticed something before that. Updated Answer lukeb50 631 — 6y
0
Gui Object's changed event still fires from their position updating. hiimgoodpack 2009 — 6y
0
It works, but when the player's health is lowered it gets smaller and moves it position above the frame oSyM8V3N 429 — 6y
View all comments (3 more)
0
Is this a new method? I've never seen it before. Looks neat! nicemike40 486 — 6y
0
Fairly new lukeb50 631 — 6y
0
The repeat until wouldn't run forever due to the fact that the `CharacterAdded:Wait()` will in fact return the character. It is useless though. CootKitty 311 — 6y
Ad

Answer this question