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.
