How to make background black again

I have this stamina GUI bar for my game. I added some bits of code that changes the bar that decrease’s color to red when the player is low on stamina. I tried to do a reverse of that like:

if power >= 3 then
			Bar.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
		end	

but for some reason it doesn’t work. I’ve removed it from this code, since I don’t know if that will even work. Help?

local UIS = game:GetService('UserInputService')

local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

local Player = game.Players.LocalPlayer

local NormalWalkSpeed = 13

local NewWalkSpeed = 23

local power = 10
	
local sprinting = false

local Character = Player.Character



repeat wait() until game.Players.LocalPlayer.Character

local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://913376220'
RunAnim = Character.Humanoid:LoadAnimation(Anim)


UIS.InputBegan:connect(function(key, gameProcessed) 
	
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		
		RunAnim:Play()
		
		Player.PlayerGui.MainGUI.StatusFrame.CaloriesScript.DecreaseValue.Value = 3
		
		Player.PlayerGui.MainGUI.StatusFrame.HydrationScript.DecreaseValue.Value = 3
		
		Character.Humanoid.WalkSpeed = NewWalkSpeed
	
		sprinting = true
		
		

		while power > 0 and sprinting do

			power = power - .03

			Bar:TweenSize(UDim2.new(power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)
			
			
			wait()

			if power <= 0 then -- less than or equal to 0
				
				Character.UpperTorso.HeavyBreathing:Play()
				
				RunAnim:Stop()
				
				Bar.BackgroundColor3 = Color3.fromRGB(203, 0, 0) -- changes color to red
				
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
				
				
			end

		end

	end

end)


UIS.InputEnded:connect(function(key, gameProcessed)

	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		
		RunAnim:Stop()
		
		
		Player.PlayerGui.MainGUI.StatusFrame.CaloriesScript.DecreaseValue.Value = 15

		Player.PlayerGui.MainGUI.StatusFrame.HydrationScript.DecreaseValue.Value = 10
		
		
		Character.Humanoid.WalkSpeed = NormalWalkSpeed

		sprinting = false
	
		
		while power < 10 and not sprinting do
			
			RunAnim:Stop()
			
			power = power + .01

			Bar:TweenSize(UDim2.new(power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)

			wait()

			if power <= 3 then -- less than or equal to 3
				RunAnim:Stop()
				
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
				
			end

		end

	end
	
end)


1 Like

So there’s aren’t any errors in the output? Maybe making it wait can fix it.

if power >= 3 then
	task.wait() --Change the time if you need
	Bar.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
end	

That didn’t work. I added it at the end btw

local UIS = game:GetService('UserInputService')

local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

local Player = game.Players.LocalPlayer

local NormalWalkSpeed = 13

local NewWalkSpeed = 23

local power = 10
	
local sprinting = false

local Character = Player.Character



repeat wait() until game.Players.LocalPlayer.Character

local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://913376220'
RunAnim = Character.Humanoid:LoadAnimation(Anim)


UIS.InputBegan:connect(function(key, gameProcessed) 
	
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		
		RunAnim:Play()
		
		Player.PlayerGui.MainGUI.StatusFrame.CaloriesScript.DecreaseValue.Value = 3
		
		Player.PlayerGui.MainGUI.StatusFrame.HydrationScript.DecreaseValue.Value = 3
		
		Character.Humanoid.WalkSpeed = NewWalkSpeed
	
		sprinting = true
		
		

		while power > 0 and sprinting do

			power = power - .03

			Bar:TweenSize(UDim2.new(power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)
			
			
			wait()

			if power <= 0 then -- less than or equal to 0
				
				Character.UpperTorso.HeavyBreathing:Play()
				
				RunAnim:Stop()
				
				Bar.BackgroundColor3 = Color3.fromRGB(203, 0, 0) -- changes color to red
				
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
				
				
			end

		end

	end

end)


UIS.InputEnded:connect(function(key, gameProcessed)

	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		
		RunAnim:Stop()
		
		
		Player.PlayerGui.MainGUI.StatusFrame.CaloriesScript.DecreaseValue.Value = 15

		Player.PlayerGui.MainGUI.StatusFrame.HydrationScript.DecreaseValue.Value = 10
		
		
		Character.Humanoid.WalkSpeed = NormalWalkSpeed

		sprinting = false
	
		
		while power < 10 and not sprinting do
			
			RunAnim:Stop()
			
			power = power + .01

			Bar:TweenSize(UDim2.new(power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)

			wait()

			if power <= 3 then -- less than or equal to 3
				RunAnim:Stop()
				
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
				
			end

		end

	end
	
end)


if power >= 3 then
	task.wait(0.5) --Change the time if you need
	Bar.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
end	

The if statement isn’t being constantly checked. Maybe include it in a loop so the code will always run when it needs to.

I don’t know how to loop but something like this?

while true do
if power >= 3 then
	task.wait(0.5) --Change the time if you need
	Bar.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
	end	
	end

Why don’t you include it in this loop?

It’s not good to create unnecessary loops as it can affect performance

That won’t work because it’s the same code that controls regenerating the stamina bar, and it will only work if power >= 3

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
    if power >= 3 then
		Bar.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
	end	
end)
1 Like

Sorry to respond late, but what does this do that’s different from the laggy while true do?

I don’t know(extra characters). RenderStepped updates every frame