How to change color of health bar when health decreases

When health is low color is red and when its full its green and it slowly turns red and will be yellow or orange in middle

1 Like

you could change the rgb values depending on the health like when its lower the less green there is when its lower the more red it has

Your question is difficult to understand. But, if it’s a frame or related GUI object, use Color3.new().

Are you talking about the built-in Roblox health bar?

If you are talking about a custom health bar GUI, you could just have a range of health and that determines the color of the health. If you wanted to get more detailed you could tween the colors as health changes

Is this just a question or do you have a code you are looking for help with?

Code looking for help cause ik how to make a health gui but not the color script

This may help in that case … Intro to GUIs

Ik how to make guis but i am asking how to make the color change when health decreases or increases

Ok, to set the color of something, you can use Color3.new(), like I said earlier:
The script takes place as the parent of the frame:

local player = game.Players.LocalPlayer.Character or game.Players.PlayerAdded:Wait().Character
local humanoid = player:WaitForChild("Humanoid")
local frame = script.Parent

game:GetService("RunService").RenderStepped:Connect(function()
	if humanoid.Health <= 100 then
		--turn frame red
		frame.BackgroundColor3 = Color3.new(255, 0, 0)
	else
		--turn frame green
		frame.BackgroundColor3 = Color3.new(0, 255, 0)
	end
end)

no but i want it to slowly change

1 Like

That’s up to you. Remember, you shouldn’t have us make whole script systems:

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I simply created an example for you to work off.

i am not telling you to write me a health gui and color changing script only the color change

also i know how to change color in script

Set color gradiation and get from it. Only for custom one.

Now I’m confused. You said earlier:

Yet, you know how to make one now? I’m confused.

Also, it technically is a whole script if you have no idea how to program it. Try and check similar topics before you post!

I dont think you understand my question

You should look at how to tween color sequences then

1 Like

You would most likely want to use RGB values on this one:

local TweenService = game:GetService("TweenService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")
local Frame = script.Parent

local tweenInfo = TweenInfo.new(0.50, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)

local toYellow = TweenService:Create(Frame, tweenInfo, { BackgroundColor3 = Color3.new(1, 1, 0) })
local toOrange = TweenService:Create(Frame, tweenInfo, { BackgroundColor3 = Color3.new(1, 0.666667, 0) })
local toRed = TweenService:Create(Frame, tweenInfo, { BackgroundColor3 = Color3.new(1, 0, 0) })

if Humanoid.Health == 100 then
	Frame.BackgroundColor3 = Color3.new(0.333333, 1, 0)
elseif Humanoid.Health == 80 then
	toOrange:Play()
elseif Humanoid.Health == 50 then
	toYellow:Play()
elseif Humanoid.Health == 20 then
	toRed:Play()
end

One way you could do it is by lerping through a ColorSequence as @DarkDanny04 mentioned. The input would be the percentage of health the player has and the output would be the color based on such.

local hum;
local gui;

local sequence = ColorSequence.new{
	ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 0, 0)), -- 0% health color
	ColorSequenceKeypoint.new(0.5, Color3.fromRGB(255, 255, 0)), -- 50% health
	ColorSequenceKeypoint.new(1, Color3.fromRGB(0, 255, 0)) -- 100% health
}


local function GetSequencePoint(x)
	-- If we are at 0 or 1, return the first or last value respectively
	if (x == 0) then return sequence.Keypoints[1].Value end
	if (x == 1) then return sequence.Keypoints[#sequence.Keypoints].Value end
	
	-- Step through each sequential pair of keypoints and see if alpha
	-- lies between the points' time values.
	for i = 1, #sequence.Keypoints - 1 do
		local current = sequence.Keypoints[i];
		local next = sequence.Keypoints[i + 1];
		
		if (x >= current.Time and x < next.Time) then
			--Calculate how far alpha lies between the points
			local alpha = (x - current.Time) / (next.Time - current.Time);
			-- Evaluate the real value between the points using alpha
			return current.Value:Lerp(next.Value, alpha);
		end
	end
end

local function UpdateColor()
	local percent = math.clamp(hum.Health/hum.MaxHealth, 0, 1);
	gui.BackgroundColor3 = GetSequencePoint(percent);
end


hum.HealthChanged:Connect(UpdateColor)
1 Like
local Character = Plr.Character or Plr.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")
local Frame = script.Parent
if (Humanoid.Health/Humanoid.MaxHealth * 100) == 10 then -- Detect if the Hp is 10% or less
Frame.BackgroundColor3 = Color3.new(255,0,0)
elseif (Humanoid.Health/Humanoid.MaxHealth * 100) == 50 then -- Detect if the Hp is 50% or less
Frame.BackgroundColor3 = Color3.new(255,255,0)
else -- Player is in 50% + hp
Frame.BackgroundColor3 = Color3.new(64,255,0)
end```

may you have to detect the percentage