Server-Client Server Countdowns and Events

This post mostly applies to round based games and communicating the game status & countdowns to the client.

I’m currently using a remote event from the server to client to start round countdowns. One issue this has is desyncing (not much of a big deal, I think it could be fixed by sending a remote event every 30 seconds to sync countdowns with server). My current problem is cancelling the countdown loop.

E.x. the intermission countdown starts after enough players are in game, but enough players leave causing the game status to return to “Not enough players” but the client is still stuck on the intermission countdown, sending another remote event would cause the round gui text to overlap. What would be a proper way of handling such things?

-- Client sided, local script
function doCountdown(givenTime)
	for i = givenTime, 1, -1 do
		givenTime -= 1
		roundGUI.Text = formatTime(givenTime)
		wait(1)
	end
end

local functions = {
	NotEnoughPlrs = function()
		roundGUI.Text = "Not Enough Players"
	end,
	
	Intermission = function(timeGiven)
		doCountdown(timeGiven)
	end,
}

RemoteEvent.OnClientEvent:Connect(function(funcName, timeGiven)
	functions[funcName](timeGiven)
end)

Also how are these systems usually built? Is there a better way than what I’m currently doing?

You could use a number/int value or a module script to put the countdown inside of! So you can make the countdown go server sided and change everything and what the client does is just show the number inside the value or module script.

Since this is on the client, you could use a variable that updates with the number of players every time a player leaves/joins. Just check the variable every time in the countdown loop to see if there are enough players.