Help with stamina bar

So I am trying to make a stamina bar, and for some reason, whenever the player starts sprinting, the bar spreads across half the screen.

Code in a local script:

local Player = game.Players.LocalPlayer
local Character = Player.Character

local UserInputService = game:GetService("UserInputService")

local Stamina = 250
local isRunning = false

Stamina = math.clamp(Stamina, 0, 250)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = true
		Character.Humanoid.WalkSpeed = 28
		
		while Stamina > 0 and isRunning do
			Stamina = Stamina - 1
			script.Parent:TweenSize(UDim2.new(Stamina / 250, 0, 1, 0), "Out", "Linear", 0)
			task.wait()
			if Stamina <= 0 then
				Character.Humanoid.WalkSpeed = 16
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = false
		Character.Humanoid.WalkSpeed = 16
		
		while Stamina < 250 and not isRunning do
			Stamina = Stamina + 0.5
			script.Parent:TweenSize(UDim2.new(Stamina / 250, 0, 1, 0), "Out", "Linear",0)
			task.wait()
			if Stamina == 0 then
				Character.Humanoid.WalkSpeed = 16
			end
		end
	end
end)

Hierarchy:
image

If you think you know what went wrong, please let me know. Thank you and have a wonderful day! :slight_smile:

4 Likes

Can I see the whole screen before the script, to see what the whole thing looks like? it would help a lot

1 Like

Change the Hierarchy so that the Bar is inside the background. I am assuming that Background is the background of the stamina bar. Currently you are setting the stamina bar to take up the full screen instead of the full background.

image

1 Like

That worked, but now the bar is going sideways, and I would like for it to go straight up and down.
image
Is there anyway I can fix this?

Yes, change:
script.Parent:TweenSize(UDim2.new(Stamina / 250, 0, 1, 0), "Out", "Linear",0)

To this:
script.Parent:TweenSize(UDim2.new(1, 0, Stamina / 250, 0), "Out", "Linear",0)

1 Like

That works too, I just want it to go down, and not up.
image

1 Like

script.Parent:TweenSize(UDim2.new(1, 0, -(Stamina / 250), 0), "Out", "Linear", 0)

1 Like

It still doesn’t work. Sometimes it goes off the bar and sometimes it goes down.

1 Like

just flip the blue bar 180 degrees
then use script.Parent:TweenSize(UDim2.new(1, 0, Stamina / 250, 0), "Out", "Linear", 0)

1 Like

What do you mean “flip the bar?” I’m not able to rotate it.

script.Parent.Rotation += 180
put that at the start of the code you have

found a youtube video that is related to this topic:

script.Parent.Rotation += 180
local Player = game.Players.LocalPlayer
local Character = Player.Character

local UserInputService = game:GetService("UserInputService")

local Stamina = 250
local isRunning = false

Stamina = math.clamp(Stamina, 0, 250)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = true
		Character.Humanoid.WalkSpeed = 28
		
		while Stamina > 0 and isRunning do
			Stamina = Stamina - 1
			script.Parent:TweenSize(UDim2.new(1, 0, Stamina / 250, 0), "Out", "Linear", 0)
			task.wait()
			if Stamina <= 0 then
				Character.Humanoid.WalkSpeed = 16
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = false
		Character.Humanoid.WalkSpeed = 16
		
		while Stamina < 250 and not isRunning do
			Stamina = Stamina + 0.5
			script.Parent:TweenSize(UDim2.new(1, 0, Stamina / 250, 0), "Out", "Linear",0)
			task.wait()
			if Stamina == 0 then
				Character.Humanoid.WalkSpeed = 16
			end
		end
	end
end)

It still doesn’t work. I copy and pasted the code you provided me.