How do you Reset a For Loop

What I am currently working on is this timer for my “guess the state” game. What happens is that when the player is given a question, they must submit the correct answer to get a new question and reset the timer. If the timer reaches zero, they will be given a new question to answer and the timer resets.

So as you can clearly see, the problem is that I am trying to reset a for loop (timer) with a press of a GUI button, but with no success.

Here’s the code:

--Timer--
local function timer()
	while true do
	    for i = TimerLabelValue, 1, -1 do
		    TimerLabel.Text = i
		    wait(1)
	    end
	    local NewStatePickNumber = math.random(1, 5)
		StatePickNumber = NewStatePickNumber
		chooseState()
	end
end

--Button function--
SubmitButton.MouseButton1Down:Connect(function()
	if StatePickNumber == 1 and AnswerBox.Text == "Olympia" then --Washington Correct
		CorrectSound:Play()
		AnswerBox.Text = nil
	elseif StatePickNumber == 1 and AnswerBox.Text ~= "Olympia" then --Washington NOT Correct
		IncorrectSound:Play()
	elseif StatePickNumber == 2 and AnswerBox.Text == "Salem" then --Oregeon Correct
		CorrectSound:Play()
	elseif StatePickNumber == 2 and AnswerBox.Text ~= "Salem" then --Oregeon NOT Correct
		IncorrectSound:Play()
	elseif StatePickNumber == 3 and AnswerBox.Text == "Sacramento" then --California Correct
		CorrectSound:Play()
	elseif StatePickNumber == 3 and AnswerBox.Text ~= "Sacramento" then --California NOT Correct
		IncorrectSound:Play()
	elseif StatePickNumber == 4 and AnswerBox.Text == "Carson City" then --Nevada Correct
		CorrectSound:Play()
	elseif StatePickNumber == 4 and AnswerBox.Text ~= "Carson City" then --Nevada NOT Correct
		IncorrectSound:Play()
	elseif StatePickNumber == 5 and AnswerBox.Text == "Boise" then --Idaho Correct
		CorrectSound:Play()
	elseif StatePickNumber == 5 and AnswerBox.Text ~= "Boise" then --Idaho NOT Correct
		IncorrectSound:Play()
    end
    local NewStatePickNumber = math.random(1,10)
	correctValue = true
	StatePickNumber = NewStatePickNumber
	chooseState() --Makes new question
end)
1 Like

You can use break to break the loop and restart it.

break does not restart the loop, it halts it and moves on to the next line.

But what do I actually use to reset the for loop

You could just call the timer function when it’s needed also your method of updating the text won’t work out too well using while do.

What do you mean by calling the timer function when needed?

When you need it do timer() and it will call the function

1 Like

I have stated that they must restart it themself. Never have I said break restart the loop.

1 Like