Stamina bar acting weirdly

https://streamable.com/ip9el8

i don’t think i have to explain video explains itself

--//Settings
local Maximum_Stamina = 100
local Stamina_Decrease = 0.4
local Stamina_Amount = Maximum_Stamina

local Sprint_Speed = 30
local Normal_Speed = 7

local Sprint_FOV = 100
local Normal_FOV = 70

--//Variables
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Current_Camera = workspace.CurrentCamera

--//Services
local Tween_Service = game:GetService("TweenService")
local Replicated_Storage = game:GetService("ReplicatedStorage")
local Run_Service = game:GetService("RunService")
local User_Input_Service = game:GetService("UserInputService")

--//Animations
local Sprint_Animation = script:WaitForChild("SprintAnimation")
local Play_Sprint_Animation = Humanoid:LoadAnimation(Sprint_Animation)

--//Gui
local Background = script.Parent.BackGround
local Bar = Background.Bar

--//Booleans
local IsSprinting = false

local function FOV_Tween(duration, FOV)

	local tween = Tween_Service:Create(Current_Camera, TweenInfo.new(duration,Enum.EasingStyle.Quad), {FieldOfView = FOV})
	tween:Play()
	spawn(function()
		
		tween.Completed:Wait()
		tween:Destroy()
	end)
end

--//Key Pressed Function
User_Input_Service.InputBegan:Connect(function(Input_Object, GameProcessed)
	if Humanoid.MoveDirection.Magnitude > 0 then
	if Input_Object.KeyCode == Enum.KeyCode.LeftShift and not GameProcessed then
		if not IsSprinting then
				IsSprinting = true
				Humanoid.WalkSpeed = Sprint_Speed
				FOV_Tween(0.8, Sprint_FOV)
				
				if not Play_Sprint_Animation.IsPlaying then
					Play_Sprint_Animation.Looped = true
					Play_Sprint_Animation:Play()
					
				end
			end
		end
	else
		if Play_Sprint_Animation.IsPlaying then
			Play_Sprint_Animation:Stop()
			Humanoid.WalkSpeed = Normal_Speed
		end
	end
end)


User_Input_Service.InputEnded:Connect(function(Input_Object)

	if Input_Object.KeyCode == Enum.KeyCode.LeftShift then
			IsSprinting = false

		if  Play_Sprint_Animation.IsPlaying then
					Play_Sprint_Animation:Stop()

				end
	
		Humanoid.WalkSpeed = Normal_Speed
		FOV_Tween(2, Normal_FOV)
			end
end)

Run_Service.RenderStepped:Connect(function()
	if Stamina_Amount > 0 then
	if IsSprinting == true and Humanoid.MoveDirection.Magnitude > 0 then
		Stamina_Amount =- Stamina_Decrease
		end
	end
	if Stamina_Amount < Maximum_Stamina then
		Background.Visible = true
		
		local Formula = Stamina_Amount/Maximum_Stamina * 100
		Bar:TweenSizeAndPosition(UDim2.new(1, 0, Formula, 0), UDim2.new(1, 0, 1 - Formula, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
		if not IsSprinting then
			Stamina_Amount += Stamina_Decrease
		end
	else
		Background.Visible = false
	end
end)
1 Like

I think you miscalculated the formula for the stamina percentage. Scale is just a value between 0 1, it’s just a percentage of the position. So actually you don’t need that * 100.

Fixed version:

local Formula = Stamina_Amount/Maximum_Stamina

1 Like

now its better but the positioning of Bar is weird

1 Like

Why are you subtracting 1 from the formula for the position try removing that.

1 Like

so it wont do this

On the bar, there is a ClipDescendants property that you can turn on. Try turning that on and the frame won’t go below.

it didnt work the gui now becomes just small

Set the X value in the position to 0. You have it set to 1, which ends up moving the bar to the right.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.