Another way to handle status updates in round based code?

I’m wondering if there was a way to handle status updates that are essentially the same every round loop. I currently have a function that takes a couple of arguments, but the lines that call the function are quite long.

local function UpdateStatus(Status, Yield, YieldTime)
	ReplicatedStorage:SetAttribute('Status', Status)
	
	if Yield then
		wait(YieldTime)
	end
end

while true do
	local Interval = Settings.Interval
	local RequiredPlayers = Settings.RequiredPlayers
	
	if #PlayersService:GetPlayers() < RequiredPlayers then
		UpdateStatus('Not enough players to start', true, Interval)
		
		continue
	end
	
	for Timer = Settings.IntermissionTimer, 1, -1 do
		UpdateStatus(('Starting new round %.2d:%.2d'):format(Timer / 60, Timer % 60), true, 1)
	end
	
	for Timer = Settings.Grace, 1, -1 do
		UpdateStatus(('Grace period %.2d:%.2d'):format(Timer / 60, Timer % 60), true, 1)
	end
	
	for Timer = Settings.RoundTimer, 1, -1 do
		if #Players <= 1 then
			break
		end
		
		UpdateStatus(('Time remaining %.2d:%.2d'):format(Timer / 60, Timer % 60), true, 1)
	end
	
	if #Players > 1 then
		UpdateStatus('Time is up! There is no winner.', true, Interval)
	else
		local _, Winner = next(Players)

		if Winner then
			UpdateStatus(('Winner: %s!'):format(Winner.Name), true, Interval)
		else
			UpdateStatus('There was no winner!', true, Interval)
		end
	end
end

I removed most of the code that doesn’t call UpdateStatus.

1 Like

Edit: I realized I don’t need to pass a Yield argument, I can just pass YieldTime as 0 and check if it is greater than 0 inside the UpdateStatus function:

local function UpdateStatus(Status, YieldTime)
	ReplicatedStorage:SetAttribute('Status', Status)
	
	if YieldTime > 0 then
		wait(YieldTime)
	end
end

Still, if anyone has another method or any critiques I’m down to make some changes.