While loop not stopping

Hi! I’m making a game that counts how long it takes to complete an obstacle course. The problem is that my while loop won’t stop! Here’s the code I use (local script in starter player scripts):

local debounceTimerCount = true
local timeCheck = 0
local boolCheck = game.ReplicatedStorage["Start Game"].StartCharacterCheck.Value
game.ReplicatedStorage["Start Game"].OnClientEvent:Connect(function()
	boolCheck = true
	boolCheck = not boolCheck
	timeCheck = 0
	while not boolCheck do
		if debounceTimerCount then
			debounceTimerCount = not debounceTimerCount
			timeCheck += 1
			print(timeCheck)
			wait(1)
			debounceTimerCount = true
		end
		
	end
	game.ReplicatedStorage.GameEnd.OnClientEvent:Connect(function()
	boolCheck = true
	game.ReplicatedStorage.TweenDetails:WaitForChild("SecondsTaken").Value = timeCheck
	print(timeCheck)
	game.ReplicatedStorage.TweenDetails:FireServer(workspace)
	end)	
end)

Thanks,
NeoGaming_RBLX

Put this above the loop, because it won’t actually run until the loop ends otherwise.

3 Likes

It works! Thanks for the help :slight_smile:
EDIT: Studio is freezing and it says it’s because of the while loop

you could use break to stop the while loop

The output is saying

Script timeout: exhausted allowed execution time

How would I fix this?

1 Like

Put a wait in the beginning of the while loop , incase the if statement is not executed because the condition is not met there will be no waiting , which breaks your script.

Well, If You Need To Stop Looping “While” I’ll Show You My Example Because Your Script Confused Me.


local something = true

while something == true do
	print("something is true")
	something = false
	if something == false then
		break -- This Method Breaks The Loop.
	end
end

The only problem is that the output prints one more second than the real time it took. But it still works, and that’s what counts :slight_smile: