Stamina System faulty

Hello! Hope your having a good day!

I’ve encountered problem with my stamina,

The While loop wont end after I end my UIS.


player.Character:WaitForChild("Humanoid").Running:Connect(function()
	
	if Character:WaitForChild("Humanoid").MoveDirection.Magnitude > 0 then
		UIS.InputBegan:Connect(function(IN, IS)
			
			if IS then return end
			
			if IN.KeyCode == RunningKey then
				
				if player.StaminaArea.Stamina.Value > 0 then

					Character:WaitForChild("Humanoid").WalkSpeed = speed
					
					YK = true
					
					while player.StaminaArea.Stamina.Value >= 0  and task.wait(.2) do
						
						task.wait(.2)

						player.StaminaArea.Stamina.Value -= 1  --Call this the Minus Value
						
						if YK == false then
							break
						end
						
					end
				end
				
			end
		end)
		
		UIS.InputEnded:Connect(function(IN, IS)

			if IS then return end

			if IN.KeyCode == RunningKey then
				
				Character:WaitForChild("Humanoid").WalkSpeed = NS
				
				YK = false
				
			end
		end)
	else
		UIS.InputEnded:Connect(function(IN, IS)

			if IS then return end

			if IN.KeyCode == RunningKey then

				Character:WaitForChild("Humanoid").WalkSpeed = NS
				
				YK = false
			end
		end)
	end
end)

Every time the UIS is cancelled, the Minus value is timed by 2 each time its activated.
It has been bugging me ever since i thought of the idea and wish this is fixed real soon.

Thank you.

Just doubled checked again and the problem is when the character moves and stays still.

But Im unable to fix it

task.wait has a nasty habbit of causing while loops to break like that sometimes, so I’d try replacing it with a normal wait command to see if that works

Sorry for the late reply,

I’ve just tested it out and it still displays the same problem,
I have the loop fixed and working, its just that I don’t how to enable it only when you start walking.