Breaking out of loops

I want to be able to break out of this loop instantly and not have to wait for it to loop again and check if the values are true so it can break. Here is my code:

-- loop
for i = gamelength, 0, -1 do
		_time.Value = i
		
		if i == 0 then
			results.NoTime:Fire()
		end
		
		if #pig <= 0 then
			results.NoPlayers:Fire()
		end
		
		-- checking for break
		if notime or playerreachedtop or noplayers then
			break
		end

		task.wait(1)
	end

What should I do? The values that are checked are set to true via bindable events.

Thanks in advance.

change the task.wait(1) to a loop that compares time passed through the tick() or time() function. have it break out of THAT loop once one of the conditions are met or the desired time has passed. you’d have to nest the loop.

2 Likes

sorry to be a pain but could you maybe post what this would look like. thanks for the help

replace task.wait(1) with something like

local StartTime = time()
local WaitTime = 1 -- time to wait, 1 second in this case

repeat
   task.wait()
until time() - StartTime >= WaitTime or notime or playerreachedtop or noplayers
1 Like

thank you so much this has been annoying me forever

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