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

Script that is supposed to change color of a health bar depending on health isnt working?

Asked by 6 years ago
Edited 6 years ago

I have a script thats supposed to change a custom health bars color depending on the current health of the player.

But unfortunately its not working, there is also no error?

Heres the code

wait(1)
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HealthBar = script.Parent.HealthBar
local Background = script.Parent

if Humanoid.Health >= 76 then
    HealthBar.BackgroundColor3 = Color3.new(234/255, 234/255, 0/255)
elseif Humanoid.Health <= 75 then
    HealthBar.BackgroundColor3 = Color3.new(255/255, 170/255, 0/255)
elseif Humanoid.Health <= 50 then
    HealthBar.BackgroundColor3 = Color3.new(217/255, 0/255, 0/255)
end

function Update()
    local HP = Humanoid.Health / Humanoid.MaxHealth
    script.Parent.HealthNumber.Text = math.floor(Humanoid.Health + .5)
    HealthBar:TweenSize(UDim2.new(HP, 0, 1, 0), "Out", "Elastic", .1, true)
    wait(.1)
end

Update()

Humanoid.Changed:connect(Update)

anyone have any ideas?? Thank you for your time :)

0
Well, you made the healthbar, so I assume you know the directory to the healthbars color, maybe try double checking, reading over the code carefully PhaseBlaze -7 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

You're only running the color-changing code once, that's why. You need to put lines 10-16 into the Update() function so it runs that code every time the player's health changes, not once.

Ad

Answer this question