How to make a stamina bar change colors depending on how much stamina the player has

I wanna make my stamina bar adjust color from blue to red depending on how much stamina the player has. I know I can use Color3.fromHSV but I don’t know how to do it. Can anyone help me?

You could alternatively just use the interpolation function :Lerp which is simpler to use

local blue = Color3.new(0, 0, 1)
local red = Color3.new(1)

local clr = red:Lerp(blue, stamina) --stamina being a 0 to 1 value, 1 being max stamina
1 Like

Try using Color3:Lerp(). This function smoothly interpolates the two colors:

local Blue = Color3.new(0,0,1)
local Red = Color3.new(1,0,0)

print(Blue:Lerp(Red,0.5)) -- This color would be halfway between red and blue
print(Blue:Lerp(Red,0)) -- This would be just blue, since you're translating it 0%
print(Blue:Lerp(Red,1)) -- This would be fully red, since you're translating it to red by 100%
1 Like

I tried this and it didn’t work, it just flashes the color bar

Basically it keeps flashing teal and white

Yea it would be really helpful if you showed me the script

The fraction alpha argument (2nd argument) to Color3:Lerp should be a value between 0 and 1 representing the percentage of stamina remaining, i.e; 50% stamina would be represented by a fraction alpha of 0.5.

Color3.new(1, 0, 0):Lerp(Color3.new(0, 1, 0), CurrentStamina/MaxStamina)
Something like this.

Ok sure

local blue = Color3.new(0.172549, 0.572549, 1); -- light blue
local red = Color3.new(0.368627, 0, 0); -- dark red

stamina:GetPropertyChangedSignal("Value"):Connect(function()
	tweenservice:Create(staminabar, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Size = UDim2.new(stamina.Value / 100, -5, 1, -5)}):Play();
	staminabar.BackgroundColor3 = red:Lerp(blue, stamina.Value)
end);

And what is stamina.Value? I believe I mentioned in my first post that the interpolation alpha must be a 0 to 1 number
@Forummer explained above in greater detail

stamina.Value should be divided by the maximum stamina value.

2 Likes

Oh okay, I will do that

(char limit)

It works now, thank you so much