Script doesn't teleport all the players

  1. What do you want to achieve? Keep it simple and clear!
    This script that I made should teleport each player to a different spawn.

  2. What is the issue? Include screenshots / videos if possible!
    The script should teleport all the players. But it just teleports one player.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried adding local variables and using :GetPlayers instead of :GetChildren in that line:
    for i,v in pairs(game:GetService(“Players”):GetPlayers()) do

But it still didn’t work.

Script:

local spawns = {}

function AddSpawnsToTable()

local SpawnsFolder = workspace.CurrentMap:FindFirstChild("Map")

for i,v in pairs(SpawnsFolder.Spawns:GetChildren()) do
table.insert(spawns, v)
end
end

function TeleportPlayersMap()
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
local character = v.Character
local randomSpawn = spawns[math.random(1, #spawns)]
character.HumanoidRootPart.CFrame = CFrame.new(randomSpawn.Position)
table.remove(spawns, randomSpawn)
end
end

AddSpawnsToTable()
wait(1)
TeleportPlayersMap()
1 Like
game.ReplicatedStorage.BombStartGui:FireClient(randomPlayer)

This is firing an event to another server. Can you show the server-side script to compare to this?
Thanks.

1 Like

Now I have checked the console and the next error appears:

invalid argument #2 to ‘remove’ (number expected, got Instance)

The error seems to be here:

function TeleportPlayersMap()
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
	local character = v.Character
	local randomSpawn = spawns[math.random(1, #spawns)]
	character.HumanoidRootPart.CFrame = CFrame.new(randomSpawn.Position)
	table.remove(spawns, randomSpawn)
  end
end
1 Like

When you use table.remove it’s going to look for a numerical index, so your best bet to remove this from the table is to just do

local function TeleportPlayersMap()
	for i,v in pairs(game:GetService("Players"):GetPlayers()) do
		local char = v.Character
		local randomSpawn = math.random(1, #spawns) -- this will return the numerical index of the randomspawn in the table
		
		char.HumanoidRootPart.CFrame = CFrame.new(spawns[randomSpawn].Position)
		table.remove(spawns, randomSpawn)
	end
end

see if that works if not reply back.

Edit : just realized that this wont work so ill just fix the function

1 Like