Getting coroutines to not run at the same time / not "skip"?

I’m trying to write a phase cycle system that goes through each “stage” of the game on a loop a set number of times. This works fine, however I modified it to use coroutines so that I could have a manual control panel that allows me to override which phase the game is currently on.
I’ve run into a few problems, but the current one is that I have my NORMAL phase countdown code as a coroutine, which seems to be causing all of the phases to run, instantly, at the same time (ignoring a “wait” in the coroutine itself), and causing the game cycle loop to skip over. The only message I get in the console is a yellow warning, “task wait should not be called on a thread that is already ‘waiting’ in the task library”, but I’m not sure what this means or if it’s even that important.

How can I get these “phases” to function normally (wait for 5 seconds and show up on my timer before changing), and let me interrupt them at the click of a button?

Current main game cycle code without all my comments (I use a lot of them):

local mainGameCycleCoro = coroutine.create(function()
	for i=1, 10, 1 do
		for phase,length in phaseLengthDict do
			coroutine.resume(regPhaseCountdownCoro, length, phase, i)
			--this little for loop appears to skip instantly.
		end
	end
	-- bit down here is not faulty
	statusText = "DRAW: 10 days reached." --default ending unless smth interesting has happened
	timerText = "GAME END"
	statusUpdate:FireAllClients(statusText)
	endRound()
end)

I don’t think this bit needs to be a coroutine but I just have it that way because I was trying to yield it earlier.

Regular phase countdown coroutine (this is the one that’s confusing me):

local regPhaseCountdownCoro = coroutine.create(function(timerLength, phaseText, loopNum)
	--cut out a chunk of irrelevant code here
	statusText = phaseText .. " " .. tostring(loopNum)
	statusUpdate:FireAllClients(statusText)
	-- shouldnt be faulty
	for i=timerLength, 0, -1 do
		local timerMinute = (i // 60)
		local timerSecond = (i % 60)
		if timerSecond < 10 then
			timerText = tostring(timerMinute)..":0"..tostring(timerSecond)
		else
			timerText = tostring(timerMinute)..":"..tostring(timerSecond)
		end
		timerUpdate:FireAllClients(timerText)
		-- this main part of the for loop to do with timer text should not be faulty.
		task.wait(1)
		-- this wait seems to get skipped?
	end
end)

Apologies for spaghetti code.

coroutine.resume starts immediately executing the thread, and the thread which called coroutine.resume isn’t doing anything until the coroutine it has resumed yields, returns or errors. In your case, task.wait yields the thread, and so coroutine.resume in mainGameCycleCoro returns. This makes it continue looping and it resumes the regPhaseCountdownCoro thread instead of the task.wait resuming it.

Essentially, mainGameCycleCoro starts executing regPhaseCountdownCoro, the coroutine does task.wait, and since you didn’t make it explicitly wait for regPhaseCountdownCoro to finish (coroutine.resume returns when it yields), it resumes it again, and it makes it appear that the phases are “skipped”.

To fix this, you need to have some mechanism in place so that mainGameCycleCoro waits for the regPhaseCountdownCoro coroutine to finish. Perhaps you can use coroutine.yield() in mainGameCycleCoro, and it gets resumed by regPhaseCountdownCoro (though that would not work if there wasn’t any task.wait in regPhaseCountdownCoro)

Ooh, I see. The task.wait is because this is mostly a countdown program that both dictates how long this “game phase” runs for and also a timer on screen. Is there anything better I could use to achieve this countdown (edit) or would the countdown have to execute inside of my for loop, mostly eliminating the need for regPhaseCountdownCoro in the first place?

Minor tweaks later (mostly just adding small pieces of code), the issue is now that it doesn’t appear to do anything at all upon initiating the main game phase, with absolutely nothing in the console:

for phase,length in phaseLengthDict do
			local coroResult = coroutine.resume(regPhaseCountdownCoro, length, phase, i)
			while coroResult ~= "Cycle success" do
				local coroResult = coroutine.resume(regPhaseCountdownCoro) --continue?
			end
		end

This is the tweaked function; the goal of my little while loop here is to make it keep resuming regPhaseCountdownCoro until it successfully gets past the task.wait, which I think probably isn’t working too well but it’s the best I have right now.