Continue while true loop when condition is false and becomes true again

Henlo frens!

I’m making a sprint script for my game but it appears i am in abit of a pickle.

When i hold down shift and hold any of the movement keys, the stamina should decrement by 1 every 0.1 seconds. And when i let go of the movement keys (with shift still held down) the stamina should stop decrementing. Very simple to make, yes… However, my greatest enemy known only as “My inability to problem solve” has come back to haunt your great hero once again.

While it is true that when i release the movement key while holding shift, the stamina stops decrementing. When i press the movement keys again, the stamina doesnt continue decrementing!!! Shocking. I know.

With that being said here is my script:

local stamina = 100

userInput.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		cameraFovTweenOut:Play()
		crouchwalkAnimTrack:Stop()
		crouchAnimTrack:Stop()
		crouching = false
		running = true
		if humanoid.MoveDirection.Magnitude ~= 0 then
			humanoid.WalkSpeed = 25
			cameraFovTweenIn:Play()
			while running do wait(0.1)
				stamina -= 1
				print(stamina)
				if humanoid.MoveDirection.Magnitude == 0 then
					running = false
				end
				
				if running == false and humanoid.MoveDirection.Magnitude ~= 0 then
					running = true
				end
				
				userInput.InputEnded:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.LeftShift then
						cameraFovTweenOut:Play()
						running = false
						humanoid.WalkSpeed = 16
					end
				end)
			end
		end
	end
end

TL;DR
Condition 1: When i hold shift and WASD my stamina goes down(Intentional)
Condition 2: When i keep holding shift and release WASD my stamina stops going down(Intentional)
Condition 3: when i still keep holding shift and press WASD again my stamina doesnt go down like in condition 1 like its supposed to (Problem)

PS: I havent coded in the part where you regenerate stamina or stop sprinting when you run out of stamina so for now stamina is just a number that goes down when you sprint

The problem is because in the while loop for running, you check the magnitude, and if its == 0 you turn your running = false. This breaks out of your loop, so it will not think you are running until you release and press shift all over again.

Try this and see if it helps you out.

local walkspeed = 16
local runSpeed = 25
local staminaRemove = .1 --per second

userInput.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = true
		humanoid.WalkSpeed = runSpeed
	end
end)

userInput.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = false
		humanoid.WalkSpeed = walkspeed
	end
end)

local eTime = 0
while true do
	if running then
		if humanoid.MoveDirection.Magnitude ~= 0 then
			Stamina = math.max(0,Stamina-(eTime*staminaRemove))
		end
	end
	eTime = task.wait()
end
2 Likes

Works perfectly, thank you fren. Hope you have a wonderful day

1 Like

You can mark their answer as a solution if you want

mb forgot to do that, thank you.

1 Like

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