Round system won't teleport all players at point

Hello, I am making a game with a round system. Everything works, it does spawn the players on the map. Problem is, it doesn’t sometimes spawn a few players, and keeps them at spawn.

Snippet code:

local points = {}
		for i,v in pairs(chosen:WaitForChild("Spawns"):GetChildren()) do
			table.insert(points, v)
		end
		for _,v in pairs(points) do
			local val = Instance.new("BoolValue")
			val.Name = "Taken"
			val.Value = false
			val.Parent = v
		end
		Text.Value = "SETTING UP ROUND.."
		for i,pl in pairs(playing) do
			local add = coroutine.wrap(function()
				local char = pl.Character
				if char then
					pcall(function()
						local function entries()
							local picked = points[math.random(#points)]
							if picked.Taken.Value == false then
								picked.Taken.Value = true
								char.HumanoidRootPart.Position = picked.Position
								char.HumanoidRootPart.Anchored = true
							else
								return "taken"
							end
						end
						local MAX_ENTRIES = 0
						local s, r = pcall(function()
							entries()
						end)
						if r == "taken" then
							repeat
								local s, g = pcall(function()
									entries()
								end)
								MAX_ENTRIES += 1
								if MAX_ENTRIES >= 20 then
									pcall(function()
										pl.Team = game.Teams.Lobby
										table.remove(playing, i)
										PlayersLeft.Value = tostring(#playing)
										pl.Character.Humanoid:TakeDamage(math.huge)
									end)
									break
								end
							until g ~= "taken" or MAX_ENTRIES >= 20
						end
					end)
				end
			end)
			add()
2 Likes

Addtional Info:

(T = True, F = False)

2 Likes

Is there perhaps a gap between for i,pl in pairs(playing) do the players table being populated and it being iterated over? If so, players may have joined in between.

Well, there isn’t really and gaps, and I have events set up that if a player leaves; it removes them from the table. 1 line before the for i,pl in pairs(playing) do event, I have 3 lines of code set to insert all the players into the playing folder. I also have it so it loops through the table, sets up a entire coroutine on the player.

Here is the entire full round system code:


----------

local modifiers = {
	["Night"] = function()
		game:GetService("TweenService"):Create(game.Lighting, TweenInfo.new(4), {ClockTime = 0}):Play()
	end,
	

	["Night"] = function()
		game:GetService("TweenService"):Create(game.Lighting, TweenInfo.new(4), {ClockTime = 0}):Play()
	end,
	
	["Night + Fog"] = function()
		game:GetService("TweenService"):Create(game.Lighting, TweenInfo.new(4), {ClockTime = 0}):Play()
		game:GetService("TweenService"):Create(game.Lighting, TweenInfo.new(4), {FogColor = Color3.new(0,0,0)}):Play()
		game.Lighting.FogStart = 20
		game.Lighting.FogEnd = 50
	end,
	
	["One_HP"] = function(t)
		for i,v in pairs(t) do
			pcall(function()
				v.Character.Humanoid.Health = 1
			end)
		end
	end,
	
}

local mod = {}
for i,v in pairs(modifiers) do
	table.insert(mod, i)
end

local RS = game.ReplicatedStorage
local RINP = RS:WaitForChild("RoundInProgress", 5)
local Winner = RINP:WaitForChild("Winner", 5)
local PlayersLeft = RINP:WaitForChild("PlayersLeft", 5)
local Intermission = RS:WaitForChild("Intermission", 5)
local Time = Intermission:WaitForChild("Time", 5)
local Text = RS:WaitForChild("Text", 5)
local TimeLeft = RINP:WaitForChild("TimeLeft", 5)
local ModifiersChances = script:WaitForChild("ModifiersChances", 5)

RINP.Changed:Connect(function()
	if RINP.Value == false then
		Text.Value = "ROUND OVER! [" .. Winner.Value .. " WINS]"
	elseif RINP.Value == true then
		local update = coroutine.wrap(function()
			repeat
				if TimeLeft.Value ~= "120" then
					Text.Value = "Round in progress [ALIVE: " .. PlayersLeft.Value .. "] (" .. TimeLeft.Value .. ")"
				else
					Text.Value = "Round in progress [ALIVE: " .. PlayersLeft.Value .. "]"
				end
				task.wait(.02)
			until tonumber(PlayersLeft.Value) <= 1
		end)
		update()
	end
end)

while task.wait() do
	if #game.Players:GetPlayers() > 1 then
		local playing = {}
		Intermission:WaitForChild("Time", 5).Value = Intermission:WaitForChild("Time", 5):WaitForChild("UsualIntermission", 5).Value
		Intermission.Value = true
		if Intermission.Value == true and Time ~= "0" then
			for i = Time.Value, 1, -1 do
				Text.Value = "INTERMISSION: [" .. tostring(string.format("%d:%.02d", i/60, i%60, i*1000%1000)) .. "]"
				task.wait(1)
			end
		end
		if #game.Players:GetPlayers() < 1 then continue end
		Intermission.Value = false
		local pre
		pre = game.Players.PlayerRemoving:Connect(function(plr)
			for i,v in pairs(playing) do
				if plr == v then
					table.remove(playing, i)
					PlayersLeft.Value = tostring(#playing)
				end
			end
		end)
		local modifier = "Default"
		local random = math.random(ModifiersChances:WaitForChild("I", 5).Value, ModifiersChances:WaitForChild("I", 5):WaitForChild("I", 5).Value)
		local random2 = math.random(ModifiersChances:WaitForChild("II", 5).Value, ModifiersChances:WaitForChild("II", 5):WaitForChild("II", 5).Value)
		if random == random2 or game.ReplicatedStorage:WaitForChild("IOOChance").Value == true then
			modifier = mod[math.random(#mod)]
		else
			modifier = "Default"
	end
		Text.Value = "CHOOSING MAP.."
		task.wait(2)
		local maps = {}
		local spawns = game.ServerStorage:WaitForChild("Maps", 5)
		for i,v in pairs(spawns:GetChildren()) do
			if (v.ClassName == "Model") then
				table.insert(maps, v)
			end
		end
		local chosen = maps[math.random(#maps)]
		pcall(function()
			Text.Value = "MAP CHOSEN: [" .. chosen.Name .. "] (LOADING) {{" .. [[<font color="rgb(255,165,0)">By: ]] .. chosen.CreatedBy.Value  .. [[</font>]] .. "}}"
		end)
		task.wait(4)
		require(script:WaitForChild("NotifyAllEffect")).hint("MODIFIER IS: [" .. tostring(modifier) .. "]", 5)
		task.wait(4)
		chosen:Clone().Parent = game.Workspace:WaitForChild("Map", 5)
		local points = {}
		for i,v in pairs(chosen:WaitForChild("Spawns"):GetChildren()) do
			table.insert(points, v)
		end
		for _,v in pairs(points) do
			local val = Instance.new("BoolValue")
			val.Name = "Taken"
			val.Value = false
			val.Parent = v
		end
		task.wait(4)
		Text.Value = "SETTING UP ROUND.."
		for i,v in pairs(game.Players:GetPlayers()) do
			table.insert(playing, v)
		end
		for i,pl in pairs(playing) do
			local add = coroutine.wrap(function()
				local char = pl.Character
				if char then
					pcall(function()
						local function entries()
							local picked = points[math.random(#points)]
							if picked.Taken.Value == false then
								picked.Taken.Value = true
								char.HumanoidRootPart.Position = picked.Position
								char.HumanoidRootPart.Anchored = true
							else
								return "taken"
							end
						end
						local MAX_ENTRIES = 0
						local s, r = pcall(function()
							entries()
						end)
						if r == "taken" then
							repeat
								local s, g = pcall(function()
									entries()
								end)
								MAX_ENTRIES += 1
								if MAX_ENTRIES >= 20 then
									pcall(function()
												local player = pl
											PlayersLeft.Value = tostring(#playing)
											pl.Character.Humanoid:TakeDamage(math.huge)
										pl.Team = game.Teams.Lobby
										table.remove(playing, i)
									end)
									break
								end
							until g ~= "taken" or MAX_ENTRIES >= 20
						end
					end)
				end
			end)
			add()
		end
		task.wait(1.5)
		PlayersLeft.Value = tostring(#playing)
	if modifier ~= "Default" then
		for i,v in pairs(modifiers) do
			if modifier == "One_HP" then
				modifiers[modifier](playing)
			else
				modifiers[modifier]()
		end
	end
end
		for i = 3, 1, -1 do
			Text.Value = "STARTING IN: " .. tostring(i)
			task.wait(1)
		end
		for i,v in pairs(playing) do
			local success, result = pcall(function()
				v.Character.HumanoidRootPart.Anchored = false
				local connector = coroutine.wrap(function()
					local connect
					connect = v.Character.Humanoid.Died:Connect(function()
						for s, t in pairs(playing) do
							if v == t then
								t.Team = game.Teams.Lobby
								table.remove(playing, s)
							end
						end
						PlayersLeft.Value = tostring(#playing)
						connect:Disconnect()
					end)
				end)
				connector()
				game.ServerStorage:WaitForChild("Swords", 5):WaitForChild("LinkedSword", 5):Clone().Parent = v.Backpack
				v.Team = game.Teams.Playing
			end)
			if not success then
				print("ERROR: " .. result)
			end
		end
		RINP.Value = true
		local PLV
		PLV = PlayersLeft.Changed:Connect(function()
			local BadgeService = game:GetService("BadgeService")
			if #playing <= 1 or tonumber(PlayersLeft.Value) <= 1 then
				local play = coroutine.wrap(function()
					local sound = Instance.new("Sound")
					sound.Volume = 1
					sound.SoundId = "rbxassetid://1836287134"
					sound.Parent = game.Workspace
					sound:Play()
					sound.Ended:Wait()
					sound:Destroy()
				end)

				for i,v in pairs(playing) do
					if not BadgeService:UserHasBadgeAsync(v.UserId, 2126795254) then
						local success, result = pcall(function()
							BadgeService:AwardBadge(v.UserId, 2126795254)
						end)
						if success then
							game.ReplicatedStorage:WaitForChild("Broadcast"):FireAllClients(v.Name .. " has earned the badge; First win!", Color3.fromRGB(0, 255, 255))
						end
					end
					pre:Disconnect()
					PLV:Disconnect()
					pcall(function()
						RINP.Winner.Value = v.Name
						v.Team = game.Teams.Lobby
					end)
					play()
					require(script.NotifyAllEffect).passby(RINP.Winner.Value .. " HAS WON!", Color3.fromRGB(190, 190, 0))
					if game.Lighting.ClockTime <= 1 then
						game:GetService("TweenService"):Create(game.Lighting, TweenInfo.new(2), {ClockTime = 14}):Play()
					end
					RINP.Value = false
					game.Lighting.FogEnd = 100000
					game.Lighting.FogStart = 0
					game.Lighting.FogColor = Color3.new(1,1,1)
					pcall(function()
						v.leaderstats.Wins.Value += 1
						v.Character.Humanoid:TakeDamage(math.huge)
					end)
					table.remove(playing, i)
				end
				RINP.Winner.Value = ""
				PlayersLeft.Value = "0"
				task.wait(1)
				pcall(function()
					game.Workspace:WaitForChild("Map", 5):ClearAllChildren()
				end)
			end
		end)
		if #playing <= 1 or tonumber(PlayersLeft.Value) <= 1 then
			for i,v in pairs(playing) do
				pcall(function()
					local plr = v.Character
					table.remove(playing, i)
					plr.Humanoid:TakeDamage(math.huge)
					PlayersLeft.Value = tostring(#playing)
				end)
			end
		end
		--
		--local start = coroutine.wrap(function()
		--	if #playing >= 2 then
		--		task.wait(100)
		--		for i = tonumber(TimeLeft.Value), 1, -1 do
		--			if #playing <= 1 or tonumber(PlayersLeft.Value) <= 1 then TimeLeft.Value = "120" break end
		--			TimeLeft.Value = tostring(i)
		--			task.wait(1)
		--		end
		--		pre:Disconnect()
		--		PLV:Disconnect()
		--		local play = coroutine.wrap(function()
		--			local sound = Instance.new("Sound")
		--			sound.Volume = 1
		--			sound.SoundId = "rbxassetid://1836287134"
		--			sound.Parent = game.Workspace
		--			sound:Play()
		--			sound.Ended:Wait()
		--			sound:Destroy()
		--		end)
		--		play()
		--		Text.Value = "TIMES UP!"
		--		require(script.NotifyAllEffect).passby("TIMES UP!", Color3.fromRGB(190, 190, 0))
		--		for i,v in pairs(playing) do
		--			pcall(function()
		--				v.Character.Humanoid:TakeDamage(math.huge)
		--				v.Team = game.Teams.Lobby
		--				table.remove(playing, i)
		--		end)
		--	end
		--	RINP.Winner.Value = "No one"
		--	PlayersLeft.Value = "0"
		--	task.wait(4)
		--		pcall(function()
		--		RINP.Value = false
		--		game.Workspace.Map:ClearAllChildren()
		--		end)
		--		RINP.Winner = ""
		--	end
		--end)
		--start()
		task.wait(.04)
		repeat task.wait(.04) until RINP.Value == false
	elseif #game.Players:GetPlayers() < 2 then
		Text.Value = "2 or more players are needed to start!"
		end
	task.wait(8)
	end