while wait()do repeat local G={}for t,q in pairs(game.Players:GetPlayers())do if not q:FindFirstChild("\073\110\077\101\110\117")then table.insert(G,q)end end Status.Value="\078\101\101\100 \097\116 \108\101\097\115\116 \116\119\111 \112\108\097\121\101\114\115"..(#G.."\047\050")wait(2)until#G>=2 RoundModule.Intermission(5)print("\073\110\116\101\114\109\105\115\115\105\111\110 \101\110\100 \046\046 \083\101\116\117\112 \098\101\103\105\110\110\105\110\103")local G=RoundModule.SelectChapter()local t=G:Clone()t.Name="\077\097\112"t.Parent=game.Workspace print("\077\097\112 \067\104\111\115\101\110")wait(2)local q=require(script.DoorModule)if t:FindFirstChild("\068\111\111\114\115")then q.ActivateDoors(t.Doors)else warn("\070\097\116\097\108 \069\114\114\111\114\044 \112\108\101\097\115\101 \104\097\118\101 \068\111\111\114\115 \102\111\108\100\101\114 \105\110 \116\104\101 \067\104\097\112\116\101\114 \077\097\112 \077\111\100\101\108 -- \065\108\115\111 \116\104\101 \068\111\111\114 \112\097\114\116\115 \105\110 \116\104\101 \068\111\111\114\115 \102\111\108\100\101\114")end print("\068\111\111\114\115 \083\101\116\117\112")local U={}for G,t in pairs(game.Players:GetPlayers())do if not t:FindFirstChild("\073\110\077\101\110\117")then table.insert(U,t)end end local O=RoundModule.ChoosePiggy(U)for G,t in pairs(U)do if t==O then table.remove(U,G)else game.ReplicatedStorage.ToggleCrouch:FireClient(t,true)end end RoundModule.DressPiggy(O)print("\080\105\103\103\121 \067\104\111\115\101\110")RoundModule.TeleportPiggy(O)RoundModule.TeleportPlayers(U,t.PlayerSpawns:GetChildren())print("\080\108\097\121\101\114\115 \116\101\108\101\112\111\114\116\101\100 \116\111 \109\097\112")RoundModule.InsertTag(U,"\067\111\110\116\101\115\116\097\110\116")RoundModule.InsertTag({O},"\080\105\103\103\121")print("\071\097\109\101 \083\116\097\114\116")RoundModule.StartRound(1200,O,t)U={}for G,t in pairs(game.Players:GetPlayers())do if not t:FindFirstChild("\073\110\077\101\110\117")then table.insert(U,t)end end if game.Workspace.Lobby:FindFirstChild("\083\112\097\119\110\115")then RoundModule.TeleportPlayers(U,game.Workspace.Lobby.Spawns:GetChildren())end t:Destroy()RoundModule.RemoveTags()end
-- [[[[[[[[[[[[[[[[[  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
