While loop not extending time properly

I have created a while loop timer and I would like to extend the timer by 60 seconds when someone clicks a button.
The timer gets extended but it adds a ridiculous amount of time when doing so. Each time someone votes it seems to be multiplying it a lot

Here’s the code I used:

function timerLoop()
	local i = 30
	while i >= 0 do
		if i ~= 0 then
			values.timeLeft.Value = i
			task.wait(1)
			i = i - 1
			events.updateTimer.Event:Connect(function()
				i = i + 60
			end)
		end
	end
end

Here’s an example of what’s happening:

Any help would be appreciated, thanks!

3 Likes

You’re constantly adding an event to the updateTimer every one second. So if all those events are executed, the i variable adds 1 minute for every event.

Instead, place the event before the loop.

5 Likes

This makes so much more sense, thank you for the help!

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