Handling script timeouts

I’m doing a server counter with repeat loops, but curious should the script ever get exhausted and break the whole game. Any better ways to handle it?

(function()
	coroutine.wrap(function()
		local function Start()
			Counter.Value = 600
			repeat
				print("going down")
				task.wait(1)
				Counter.Value -= 1
			until Counter.Value <= 0
			workspace:SetAttribute("GameENDED",true)
			task.wait(20)
			for _,v in Players:GetPlayers() do
				v:LoadCharacter()
			end
			Start()
			task.delay(2,function()
				workspace:SetAttribute("GameENDED",false)
			end)
		end
		Start()
	end)()
end)()

You can use promises which have built in error handling.

For the exact details you can look in the source code which uses coroutine and debug library for error tracking.

Also the advantage is there are multiple threads being created (race condition) so even if a game loop fails there is another thread which will eventually wait and restart the loop.

1 Like

Thank you, it is possible to edit the modules to only include a simple start and finish (event when the round is finished) right?

I’ve tried to use promises but the round finishes almost immediately, instead of waiting 600 seconds (or at least using a counter with intvalues)
Any ideas?

(function()
	coroutine.wrap(function()
		local function StartRound()
			return NoPromises.race({
				NoPromises.delay(600):andThenReturn(); -- Round Timer
				(function()
					print("finished!")
					workspace:SetAttribute("GameENDED",true)
				end)()
					:andThenCall(NoPromises.delay, 20) -- Intermission
					:andThen(function()
						for _,v in Players:GetPlayers() do
						v:LoadCharacter()
					end
						task.delay(2,function()
							workspace:SetAttribute("GameENDED",false)
						end)
					end)
					:andThen(function() -- can't use andThenReturn here because of conditional
						print("Restart game? Or not enough players")
						return 						StartRound()

					end)
			})
		end
		StartRound()
	end)()
end)()