How to prevent 2x timer (repost bc it didn’t got any views)

Sup!

So i wanna make a sprinting script for my and my friends game.

The thing is i am making Stamina for the Sprinting and theres a problem with while loop that if i press shift a 2nd time (i know the issue but i couldn’t find a way to fix it) its going to start another while loop and i want that it just cancels it but the stats don’t reset.

Here’s the script: (And btw… is there any better way to make the stamina regen than using while?)

local character = Player.CharacterAdded:Connect(function(character)
	local SprintAnimation = game.ReplicatedStorage.Animations:WaitForChild("Sprinthold")
	local UIS = game:GetService("UserInputService")

	

	local Completly_Stamina_dry = false -- if its completly dry u need to wait to fill it completly up

	local Stamina = 6 -- Max Stamina in Seconfs
	local StaminaUsing = false --the idea is that i can't regen if u are using it
	
	while Stamina <= 5 do
		if StaminaUsing == false then
			print(Stamina)
			Stamina = Stamina + 1
			if Stamina == 6 then
				Completly_Stamina_dry = false
				return
			end
			wait(1)
		end
		return
	end
	
	UIS.InputBegan:Connect(function(input)
		if Completly_Stamina_dry == false then
			if input.KeyCode == Enum.KeyCode.LeftShift then
				StaminaUsing = true
				Module.SprintStart(SprintAnimation, ViewModel)
				character.Humanoid.WalkSpeed = 32
				while Stamina > 0 do

					Stamina = Stamina - 1
					if Stamina == 0 then
						Completly_Stamina_dry = true
						Module.SprintStop() --for sprinting
						character.Humanoid.WalkSpeed = 16
						return
					end
					wait(1)
					print(Stamina)
					UIS.InputEnded:Connect(function(inputEnd) --should cancel it (it doesn't)
						
						if inputEnd.KeyCode == Enum.KeyCode.LeftShift then
							StaminaUsing = false
							Module.SprintStop()
							character.Humanoid.WalkSpeed = 16
							return
						else
							return			
						end

					end)
				end
			end		
		end
	end)
end)```

I’ve experienced similar issues, I would reccomend using a variable to check when the key is being pressed and use a while loop outside of the CharacterAdded event to increase stamina if it is not pressed and decrease it if it is pressed.

Personally, I would also place the script in StarterCharacterScripts so the script will automatically begin running when the character loads.

Can’t you create a variable named isSprinting and when the if input.KeyCode == Enum.KeyCode.LeftShift then if statement returned true then you want the line after add if isSprinting then (the if statement is true; like don’t worry just after if input.KeyCode == Enum.KeyCode.LeftShift then).

Just try this script:

local character = Player.CharacterAdded:Connect(function(character)
local SprintAnimation = game.ReplicatedStorage.Animations:WaitForChild("Sprinthold")
local UIS = game:GetService("UserInputService")
local Completly_Stamina_dry = false -- if it's completely dry, you need to wait to fill it completely up
local Stamina = 6 -- Max Stamina in seconds
local StaminaUsing = false -- the idea is that you can't regen if you are using it
local isSprinting = false -- keep track of whether the player is currently sprinting or not

while Stamina <= 5 do
	if StaminaUsing == false then
		print(Stamina)
		Stamina = Stamina + 1
			if Stamina == 6 then
				Completly_Stamina_dry = false
				return
			end
			wait(1)
		end
	return
end

	UIS.InputBegan:Connect(function(input)
		if Completly_Stamina_dry == false then
			if input.KeyCode == Enum.KeyCode.LeftShift then
				if isSprinting then
					StaminaUsing = false
					Module.SprintStop()
					character.Humanoid.WalkSpeed = 16
					isSprinting = false
				else
					StaminaUsing = true
					Module.SprintStart(SprintAnimation, ViewModel)
					character.Humanoid.WalkSpeed = 32
					while Stamina > 0 do
						Stamina = Stamina - 1
						if Stamina == 0 then
							Completly_Stamina_dry = true
							Module.SprintStop()
							character.Humanoid.WalkSpeed = 16
							isSprinting = false
							return
						end
						wait(1)
						print(Stamina)
						UIS.InputEnded:Connect(function(inputEnd)
							if inputEnd.KeyCode == Enum.KeyCode.LeftShift then
								StaminaUsing = false
								Module.SprintStop()
								character.Humanoid.WalkSpeed = 16
								isSprinting = false
								return
							else
								return
							end
						end)
					end
				end
			end
		end
	end)
end)