How to restart a loop if a condition is met?

In my game there is a loop. But there has to always be 2 players in the server. It starts out by waiting for at least two people but if it gets to the map voting stage and there is only 1 person in the server I just want to go back to the start of the loop. How can I do this?

you could put everything at the map voting stage and after inside a conditional (Player count > 1) so that if its not met it just skips back to the start again

1 Like

Are you talking about re-entering a loop from inside a nested loop?

-- Main game loop
while true do
    local shouldRestart = true

    -- Now you can set shouldRestart to true from
    -- inside a loop to return to the beginning of the game loop.

    if not shouldRestart then
        continue -- Return to beginning of game loop.
    end
end
2 Likes

Yes that is what I mean. 30 letter

A cleaner approach might to do a repeat until loop. That way, the loop will run once, even if the condition is already met.

-- Main game loop
while true do
    repeat
        -- Put any code that you want to run in the beginning of the game loop.
    until Players:GetPlayers() < 2
end
1 Like

So, at any point if there are less than 2 players it will restart?

So I would put the until Players:GetPlayers() < 2 at the end of the script? (400+ lines)

So you’re saying that the loop should automatically restart if there is less than 2 players, regardless of where the loop is at? I don’t think there is any shortcut for this. You’ll have to have check the number of players in the server each time you do something.

Okay, that is fine I have several parts where I can check. I just don’t know how I could restart the loop.

You can use the continue keyword to restart the loop.

4 Likes

Oh really? Just that simple? wow

1 Like

If I put the continue keyword in the if statement will it just restart the statement or restart the loop?

if AmountOfPlayers == 1 then
	continue
end

^ Inside the while loop

nvm it worked tysm! 30 letters

1 Like