Why does my script Teleport each player one by one, instead of in a group?

Hiya Developers!

I have a script here, which is meant to check what map is selected, and teleport the players to the location accordingly to the map. But, i have an issues. When 2+ players are playing, it teleports each player 1 at a time with about a 1/2 second interval.

The intended function is that, the script TP’s all players in one group?

Any ideas?

local players = game.Players:GetChildren() 
	
	
	if map.Name == "Placeholder" then 
		description.Value = "Placeholder"
		for i = 1,#players do
			if players[i].Character ~= nil then
				task.wait(3)
				local plrs = game.Players:GetChildren()
				for i = 1,#plrs do

				end
				local num = math.random(1,32) 
				plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
				plrs[i].Character.Parent = workspace.Ingame 
				print("Teleport Finished")
			end
		end
	end
	if map.Name == "Placeholder2" then 
             description.Value = "Placeholder"
				for i = 1,#players do
			if players[i].Character ~= nil then
				task.wait(3)
				local plrs = game.Players:GetChildren()
				for i = 1,#plrs do

				end
				local num = math.random(1,32) 
				plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
				plrs[i].Character.Parent = workspace.Ingame 
				print("Teleport Finished")
			end
		end
	end

Help is appreciated!

When you are looping through each player you are asking to wait 3 seconds between teleporting, reduce this duration for a faster result.

1 Like

First of all I suggest you use PivotTo() and GetPivot() instead of setting CFrame

Second, there is no function to teleport all players at once. Using a for loop will add slight delays and if you use wait, you will get more delays.

Thanks, i’ll give this a shot now and see if that works.

The reason for this is that for loops run one loop at a time, and not all at once. For example, your for loop repeats the same code over and over again for each player, not run code on every player all at once. If you wanted to teleport all the players at once, you’d set up a remote event that fires to all clients:

event:FireAllClients()

and when the players receive the event , the teleportation would be done on the client, with little to no noticeable intervals. Hope this helps! :slight_smile:

1 Like

What is the difference from CFrame and Pivot:To()?

Thanks :slight_smile:

PivotTo is newer and basically sets the CFrame.

It is extremely useful to move models and is more efficient and neater.

1 Like

FYI it’s used like this:

model:PivotTo(part:GetPivot())

You can use it for any model or part

Thanks for this, ill be sure to update my script to use this rather than CFrames

1 Like