Script only teleports one player

Hi, I’m currently trying to create a script that makes a round system that teleports players to the map after intermission is over and teleports them back once the round itself is over, my current issue is that the script only teleports one player to and from the map, there are no errors during testing nor playing the game itself.

Suggestions regarding other issues are appreciated!

--Variables
local lobbyPart = game.Workspace.SpawnLocation
local maps = game.ReplicatedStorage.Folder:GetChildren()
local status = game.ReplicatedStorage.Status
local inRound = game.ReplicatedStorage:WaitForChild("InRound")
local playersAlive = {}



local function choseMap(char, internval) -- chose map
	local mapsNo = math.random(1, #maps)
	local chosenMap = maps[mapsNo]:Clone()
	chosenMap.Parent = workspace
	print(chosenMap.Name)
	char.HumanoidRootPart.Position = chosenMap.TeleportPoint.Position

	if internval == 0 then
		task.wait(4)
		chosenMap:Destroy()
	end
end

local function round() -- start intermission and round
        local sword = game.ReplicatedStorage.ClassicSword:Clone()
	for i, v in pairs(game.Players:GetPlayers()) do
		table.insert(playersAlive, i, v)
		if inRound == true then
			inRound = false
			break
		end
		local char = v.Character or v.CharacterAdded:Wait()
		if char.Humanoid then



			local hum = char.Humanoid

			for i = 30, 0, -1 do
				status.Value = "Intermission: "..i
				task.wait(1)
			end
			choseMap(char,i)
			status.Value = "Map Chosen!"
			task.wait(5)
			sword.Parent = v.Backpack
			for i = 120, 0, -1 do -- change i to 120 when done testing, 10 during testing


				hum.Died:Connect(function()
					table.remove(playersAlive, i)
					print(playersAlive)
					if #playersAlive == 1 then
						local winner = playersAlive[i]
						status.Value = "Round Ended"
						wait(5)
						inRound = true
						if v.Name == winner.Name then

							local leaderstats = playersAlive[i].leaderstats
							local coins = leaderstats.Coins
							local wins = leaderstats.Wins
							wins.Value = wins.Value + 1
							coins.Value = coins.Value + 200
							coins.Value = coins.Value
						else
							local leaderstats = v.leaderstats
							local coins = leaderstats.Coins
							coins.Value = coins.Value + 50
						end
					end

				end)
				status.Value = "Time Left: "..i
				if i == 0 then
					sword:Destroy()
					v.Character.HumanoidRootPart.Position = lobbyPart.Position
				end
				task.wait(1)
			end
		end
	end
end


while true do
	round()
	task.wait(1)
end

put simply its because you are not using multiple threads

for i, v in pairs(game.Players:GetPlayers()) do

end

the way for loops works, is that it completes the iteration for the current index i, with v and once that index’s iteration is done, it will move to the next player in this case, but this is not always what we want, sometimes we want it done at the same time approx.

now to be honest, your code needs to be redone a bit since you have the timer within the player iteration which could cause issues when you have multiple players, you should make the main game code outside of any player thread, essentially you’ll want to add:

spawn(function()

end)

inside for loop.

About spawn though would that not cause some issues with timing due to some parts of the script running before others?

right, thats why I mentioned recoding it and having the main game timer and stuff outside of any spawn thread, to yield the script until a round is completed

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.