How to reset while loop that is called inside a function?

  1. What do you want to achieve?
    i want to reset a while loop instead of making a new one. The new one breaks my counter.

  2. What is the issue? Include screenshots / videos if possible!
    My while loop is called with a function. The function is called multiple times but the loop doesnt reset. In the code below it makes another loop when the function is called. How can i reset the loop instead of making a new one?

TouchEvent.OnClientEvent:Connect(function(reset)
	Player.leaderstats.CurrentTime.Value = 0
	while true do
		Player.leaderstats.CurrentTime.Value += 1
		Player.PlayerGui.LeaderboardGui.ScrollingFrame:WaitForChild("LeaderboardTemplate").CurrentTime.Text = Player.leaderstats.CurrentTime.Value
		wait(1)
	end
end)

(Local Script)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried using break and continue but I cant figure out how to use those in my situation.

Please note that this is my first post on the the devforum.

Okay by reset do you mean restart or do you mean completely stop the loop?

Just reset the loop. Each time i call the function, the counter adds +1 number per second.

If you mean to stop the loop before starting another one you can use task.spawn() for that:

local timerloop

if timerloop then
    task.cancel(timerloop) --if loop is already running this will cancel it
end

timerloop = task.spawn(function()
    --your loop
end)
1 Like

Thats exactly what i mean, ill check the code but roblox servers are down again…

That works perfectly, thank you very much :smiley:

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