-- [[[[[[[[[[[[[[[[[  MAIN GAME LOOP  ]]]]]]]]]]]]]]]]] --


while wait() do
    
    -- 1. Check for at least two players
    repeat
        local availablePlayers = {} -- creating an empty table to fill 

        for i, v in pairs(game.Players:GetPlayers()) do
            if not v:FindFirstChild("InMenu") then
                table.insert(availablePlayers,v)
            end
        end
        
        Status.Value = "Need at least two players"..#availablePlayers.."/2"
        
        -- need to wait or else it will crash the game
        wait (2)
        
    until #availablePlayers >= 2
    
    
    RoundModule.Intermission(5)

    print ("Intermission end .. Setup beginning")

    -- [[[[[[[[[[[[[[[[[  GAME START  ]]]]]]]]]]]]]]]]] --

    -- 2a. Choose a Map and import to the workspace
    local chosenChapter = RoundModule.SelectChapter() -- returns a map object of the chosen chapter
    local clonedChapter = chosenChapter:Clone() -- making a copy to not mess up the Chapters folder instance
    clonedChapter.Name = "Map" -- used in other scripts
    clonedChapter.Parent = game.Workspace -- put in view of the player so they can teleport to it

    print ("Map Chosen")


    -- 2b. Setup the doors
    
    
    -- wait a bit to load the doors
    wait (2)
    
    local DoorModule = require(script.DoorModule)

    if clonedChapter:FindFirstChild("Doors") then
        DoorModule.ActivateDoors(clonedChapter.Doors) -- sending in a folder
    else
        warn("Fatal Error, please have Doors folder in the Chapter Map Model -- Also the Door parts in the Doors folder")
    end

    print ("Doors Setup")

    -- 3. Choose Piggy from players NOT in the menu
    --      a player is in the menu if they did not click on 'Play' yet
    --      a player is in the menu if the player has a tag attached. the tag gets deleted/destroyed once the player clicks play
    local contestants = {} -- creating an empty table to fill 

    for i, v in pairs(game.Players:GetPlayers()) do
        if not v:FindFirstChild("InMenu") then
            table.insert(contestants,v)
        end
    end

    local chosenPiggy = RoundModule.ChoosePiggy(contestants)

    -- remove chosen piggy from list of contestants
    for i, v in pairs(contestants) do
        if v == chosenPiggy then
            table.remove(contestants,i)
        else
            -- loop through all non-piggy players
            game.ReplicatedStorage.ToggleCrouch:FireClient(v, true)
        end
    end

    -- Dress up chosen contestant as Piggy!
    RoundModule.DressPiggy(chosenPiggy)
    
    -- avoids "torso" issue when the player is falling and the map loads afterwards
    print ("Piggy Chosen")

    -- 4. Teleport Piggy and players to their spawn spots

    RoundModule.TeleportPiggy(chosenPiggy)
    RoundModule.TeleportPlayers(contestants, clonedChapter.PlayerSpawns:GetChildren())

    print ("Players teleported to map")


    -- 5. Tag players as "contestant" and as "piggy"

    RoundModule.InsertTag(contestants, "Contestant")
    RoundModule.InsertTag({chosenPiggy}, "Piggy")

    print ("Game Start")

    -- 6. Start the game!
    RoundModule.StartRound(1200, chosenPiggy, clonedChapter)
    
    -- [[[[[[[[[[[[[[[[[  GAME END  ]]]]]]]]]]]]]]]]] --

    
    -- 7. Game ended, time to clean up!
    contestants = {}
    for i, v in pairs(game.Players:GetPlayers()) do

        -- if an existing player is not in the menu, add them to the table
        if not v:FindFirstChild("InMenu") then
            table.insert(contestants,v)
        end
    end

    -- ERROR CHECK: check if the spawns folder is in the gamespace
    if game.Workspace.Lobby:FindFirstChild("Spawns") then
        RoundModule.TeleportPlayers(contestants, game.Workspace.Lobby.Spawns:GetChildren())
    end
    
    -- destroy the map
    clonedChapter:Destroy()
    
    -- clean up piggy and players (remove tags and tools)
    RoundModule.RemoveTags()
    
    
end