Need help with Color Changing Stamina bar

I made a Stamina bar that changes the color of the bar from red to green when it’s half way gone, but it never changes back to green and i’m confused on what’s a better way to format this

local HighStamColor = Color3.fromRGB(92, 193, 69)
local LowStamColor = Color3.fromRGB(191, 60, 60)


local tweenInfo = TweenInfo.new(1)
local tween = TweenService:Create(players.LocalPlayer.PlayerGui.Main.Stanima.Stanima, tweenInfo, {BackgroundColor3=HighStamColor})
local tween2 = TweenService:Create(players.LocalPlayer.PlayerGui.Main.Stanima.Stanima, tweenInfo, {BackgroundColor3=LowStamColor})
replicatedStorage.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(stamina, maxStamina)
	print(players.LocalPlayer.PlayerGui.Main.Stanima.Stanima.BackgroundColor3)
	players.LocalPlayer.PlayerGui.Main.Stanima.Stanima.Size = UDim2.new((stamina / maxStamina),0,0.9,0)
	if (stamina / maxStamina) <= 0.5 then
		if players.LocalPlayer.PlayerGui.Main.Stanima.Stanima.BackgroundColor3 == HighStamColor then
			tween2:Play()
			print"red"
		else
		end
	else
		if players.LocalPlayer.PlayerGui.Main.Stanima.Stanima.BackgroundColor3 ==  LowStamColor then
			tween:Play()
			print"green"
		else
		end
	end
end)
1 Like

Your code in general is a little messy, this isn’t a horrible thing, everyone has their own style of course.

But what I suggest doing is removing both if statements that check whether or not BackgroundColor3 is equal to something.
I don’t think you need these.

Since you’re already checking whether stamina/maxStamina is lower or higher than 0.5, you don’t need to complicate it.
Simply play the tweens immediately and see what happens. I’m not sure if this is the solution, but removing those if statements and playing the tweens right away should have the exact same functionality. Plus it looks cleaner.

Also, maybe make the tweens a function, looks neater.

The if statements are there so the tweens don’t play 100000 times