Game Loop Breaking

local PlayersService = game:GetService("Players")

local intermissionMusic = workspace.Music.IntermissionMusic
local fightMusic = workspace.Music.FightMusic
local buildMusic = workspace.Music.BuildMusic

local tickSound = workspace.Sounds.Tick
local gameTick = workspace.Sounds.GameTick
local victory = workspace.Sounds.Victory



local playersinGame = {}

local lobby = workspace.Lobby
local maps = game.ReplicatedStorage.maps:GetChildren()
local status = game.ReplicatedStorage.Status

local gameTime = 60
local intermissionTime = 5
local minPlayer = 2


swords = {
	[1] = "normal",
	[2] = "flower"
}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char.Humanoid.Died:Connect(function()
			local index = table.find(playersinGame,player)
			if index then
				table.remove(playersinGame,index)
			end
		end)
	end)
end)


game.Players.PlayerRemoving:Connect(function(player)
	local index = table.find(playersinGame,player)
	if index then
		table.remove(playersinGame,index)
	end
end)

local function musictransition(off,on)
	for i = 1, 10 do
		off.Volume -= 0.005
		task.wait(0.1)
	end
	on.Playing = true
	on.TimePosition = 0
	off.TimePosition = 0
	off.Playing = false
	for i = 1, 10 do
		on.Volume += 0.005
		task.wait(0.1)
	end
end


while true do
	for i = intermissionTime, 0, -1 do
		status.Value = "intermission :D : " .. i
		tickSound:Play()
		task.wait(1)
	end

	local numPlayers = #PlayersService:GetPlayers()

	if numPlayers < minPlayer then
		status.Value = "need more players :("
		task.wait(3)

		for i = 10, 1, -1 do
			status.Value = "waiting for more players: " .. i
			tickSound:Play()
			task.wait(1)
		end

		continue
	end

	local pickedMap = maps[math.random(1, #maps)]
	local clonedMap = pickedMap:Clone()

	musictransition(intermissionMusic,buildMusic)

	for i = 5, 0, -1 do
		status.Value = "generating map :) : " .. i
		tickSound:Play()
		task.wait(1)
	end

	status.Value = "map generated: " .. clonedMap.Name
	clonedMap.Parent = game.Workspace

	musictransition(buildMusic,fightMusic)
	for i = 5,1, -1 do
		status.Value = i
		tickSound:Play()
		task.wait(0.7)
	end
	status.Value = "GO!"


	task.wait(1)


	local tps = pickedMap.Spawns:GetChildren()
	local spawnedPlayers = {} 


	for _, player in pairs(PlayersService:GetPlayers()) do
		local character = player.Character

		if character and #spawnedPlayers < 2 then
			table.insert(playersinGame,player)
			print(playersinGame)
			local hrp = character.HumanoidRootPart
			local randomSpawn

			local classicSword = game.ServerStorage.ClassicSword
			local flowerSword = game.ServerStorage.FlowerSword

			if character.Sword.Value == 1 then
				local swordClone = classicSword:Clone()
				swordClone.Parent = player.Backpack
			end

			if character.Sword.Value == 2 then
				local swordClone = flowerSword:Clone()
				swordClone.Parent = player.Backpack

			end


			repeat
				randomSpawn = tps[math.random(1, #tps)]
			until not spawnedPlayers[randomSpawn]

			spawnedPlayers[randomSpawn] = true
			hrp.CFrame = randomSpawn.CFrame
			print(#playersinGame)
		end
	end



	local winner = nil

	for i = gameTime, 0, -1 do
		if #playersinGame <= 1 then
			if #playersinGame == 1 then
				winner = playersinGame[1]
				status.Value = "gameover! winner: ".. winner.DisplayName
				winner.leaderstats["wins πŸ†"].Value = winner.leaderstats["wins πŸ†"].Value + 1
				victory:Play()
				wait(2)
			end
			break
		else
			if i < 6 then
				status.Value = "Fight >:D : " .. i
				gameTick:Play()
			else
				status.Value = "Fight >:D : " .. i
			end


			task.wait(1)
			status.Value = "nobody won :("
		end
	end


	for i, player in pairs(PlayersService:GetPlayers()) do
		local character = player.Character
		character.Humanoid.Health = character.Humanoid.MaxHealth

		local toolbackpack = player.Backpack:FindFirstChildWhichIsA("Tool")

		if toolbackpack then
			toolbackpack:Destroy()
		end


		local charactertool = character:FindFirstChildWhichIsA("Tool")
		if charactertool	then
			charactertool:Destroy()
		end


		if character and character:FindFirstChild("HumanoidRootPart") then
			local hrp = character.HumanoidRootPart
			hrp.CFrame = lobby.Spawn.CFrame
		end
	end

	musictransition(fightMusic,intermissionMusic)


	playersinGame = {}
	clonedMap:Destroy()
end

When there is 2 players in the game its fine, but when its 3 it breaks? it stops at go and the status doesnt change from GO please help fix

1 Like

Without the specifics of where and how the code is erroring/breaking, it’s very hard to tell what is causing it to error.

1 Like
for _, player in pairs(PlayersService:GetPlayers()) do
		local character = player.Character

		if character and #spawnedPlayers < 2 then
			table.insert(playersinGame,player)
			print(playersinGame)
			local hrp = character.HumanoidRootPart
			local randomSpawn

			local classicSword = game.ServerStorage.ClassicSword
			local flowerSword = game.ServerStorage.FlowerSword

			if character.Sword.Value == 1 then
				local swordClone = classicSword:Clone()
				swordClone.Parent = player.Backpack
			end

			if character.Sword.Value == 2 then
				local swordClone = flowerSword:Clone()
				swordClone.Parent = player.Backpack

			end


			repeat
				randomSpawn = tps[math.random(1, #tps)]
			until not spawnedPlayers[randomSpawn]

			spawnedPlayers[randomSpawn] = true
			hrp.CFrame = randomSpawn.CFrame
			print(#playersinGame)
		end
	end

for _, player in pairs(PlayersService:GetPlayers()) do
		local character = player.Character

		if character and #spawnedPlayers < 2 then
			table.insert(playersinGame,player)
			print(playersinGame)
			local hrp = character.HumanoidRootPart
			local randomSpawn

			local classicSword = game.ServerStorage.ClassicSword
			local flowerSword = game.ServerStorage.FlowerSword

			if character.Sword.Value == 1 then
				local swordClone = classicSword:Clone()
				swordClone.Parent = player.Backpack
			end

			if character.Sword.Value == 2 then
				local swordClone = flowerSword:Clone()
				swordClone.Parent = player.Backpack

			end


			repeat
				randomSpawn = tps[math.random(1, #tps)]
			until not spawnedPlayers[randomSpawn]

			spawnedPlayers[randomSpawn] = true
			hrp.CFrame = randomSpawn.CFrame
			print(#playersinGame)
		end
	end

i think its breaking here but i dont know why maybe because i did this wrong? i dont know.