Help making my wave system loop work

Hey there, saints of the DevForum. I’m having trouble making my zombie defense wave system loop.

local function startWave()
    local numOfZombies = 0
    local limit = waveConfig.WAVE_CONFIGS[tostring(currentWave)]["SpawnCap"]

    while not (numOfZombies == limit) do
        spawnWaveZombies(currentWave)
        numOfZombies += 1
        print(numOfZombies)
    end
end

Start Wave function

local function endWave()
    endWaveEvent:FireAllClients()
    currentWave += 1 
    print(currentWave)
    
    return currentWave
end

End Wave function

local function gameProgression()
    task.wait(waveConfig.INTERMISSION_TIME)
    startWave()
    
    while task.wait(1) do
        if #workspace.Zombies:GetChildren() == 0 then -- This basically detects if all zombies are dead.
            endWave()
            end
            break
        else
            
        end
    end
end

gameProgression()

makeshift game progression function which I’m not very fond of.

Keep in mind that I have left out some unrelated code snippets.
Now, everything in the code samples works, but I’m not sure how to make a function loop so that after the endWave() function gets fired, the startWave() function automatically fires after.

1 Like

Why not call startWave() straight after endWave()?

Could you please elaborate? Do you mean putting startWave() into the endWave() function?

On the line after you call endWave(), try adding a delay and then calling startWave.

Ah, this does work. Just had to remove break in the function. Thanks a ton!