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

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

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

1 comment

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

1 answer

✓ Accepted answer

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.

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