How to teleport all at the same time

Can you guys check whats wrong with my code and why it teleports everyone 1 by 1? What I wanted is to teleport everyone near me somewhere and then after a couple of seconds send them back all at the same time. The problem is it keeps on teleporting everyone 1 by 1

Local Script

local ServerS = game:GetService("ReplicatedStorage")

local Player = game.Players.LocalPlayer

local UIS = game:GetService("UserInputService")
local replicated = game:GetService("ReplicatedStorage")

local dest = game.Workspace.TeleportDestination

local Ionioi = replicated:WaitForChild("Teleport")
local debounce = false
local cooldown = 1

UIS.InputBegan:Connect(function(input,isTyping)
	if isTyping then 
		return
	elseif input.KeyCode == Enum.KeyCode.Q then
		if debounce == false then
			debounce = true
			print("you pressed q")
			Ionioi:FireServer()		
		end
	end
end)
Ionioi.OnClientEvent:Connect(function()
	wait(cooldown)
	debounce = false
end)

Server Script

local players = game:GetService("Players")
local replicated = game:GetService("ReplicatedStorage")
local remote = replicated.Teleport
local destination = game.Workspace.TeleportDestination
local userdest = game.Workspace.userdest
local Tpto = game.Workspace.TeleportDestination

remote.OnServerEvent:Connect(function(player)
	if not player then return end
	local character = player.Character
	if not character then return end
	local root = character.PrimaryPart
	if not root then return end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end
	if humanoid.Health <= 0 then return end

	for _, otherPlayer in ipairs(players:GetPlayers()) do
		if not otherPlayer then continue end
		local otherCharacter = otherPlayer.Character
		if not otherCharacter then continue end
		local otherRoot = otherCharacter.PrimaryPart
		if not otherRoot then continue end
		local otherHumanoid = otherCharacter:FindFirstChildOfClass("Humanoid")
		if not otherHumanoid then continue end
		if otherHumanoid.Health <= 0 then continue end
		
		local rp =character.HumanoidRootPart
		local rp2 = otherCharacter.HumanoidRootPart

		local distance = 100
		local backdistance = 1000
		if (player.Character.HumanoidRootPart.Position - otherCharacter.HumanoidRootPart.Position).Magnitude <= distance then
			local rPoistion = rp2.CFrame
			local cPosition = rp.CFrame
			otherCharacter:SetPrimaryPartCFrame(Tpto.CFrame*CFrame.new(math.random(1,20),10,math.random(1,20)))
			character:SetPrimaryPartCFrame(userdest.CFrame*CFrame.new(0,10,0))
			character.PrimaryPart.Orientation = character.PrimaryPart.Orientation + Vector3.new(0,270,0)
			wait(10)
			if (player.Character.HumanoidRootPart.Position - otherCharacter.HumanoidRootPart.Position).Magnitude <= backdistance then
				rp.CFrame = cPosition
				rp2.CFrame = rPoistion
							
			end
			
			remote:FireClient(player)
		end
	end
end)

wait(10)

Because you’ve got this 10 second wait.

Use CTRL + F to search through your scripts.

1 Like

i would recommend teleporting everyone server side in a table btw, not the solution to the problem, just something that would improve it

As @Forummer said, the wait(10) yields the teleport script until it’s done teleporting. You could just remove the wait.

1 Like

But if I remove the 10 second wait it will instantly teleport me back. What i wanted is to teleport everyone somewhere and then after a couple of seconds they get teleported back to their previous position

Then move it outside the loop.

1 Like

Can you teach me how to do this? I’m pretty new so I’m not really good at scripting. I tried just putting it below the remote:FireClient(player) like this

			local cPosition = rp.CFrame
			otherCharacter:SetPrimaryPartCFrame(Tpto.CFrame*CFrame.new(math.random(1,20),10,math.random(1,20)))
			character:SetPrimaryPartCFrame(userdest.CFrame*CFrame.new(0,10,0))
			character.PrimaryPart.Orientation = character.PrimaryPart.Orientation + Vector3.new(0,270,0)
			if (player.Character.HumanoidRootPart.Position - otherCharacter.HumanoidRootPart.Position).Magnitude <= backdistance then				
			end
			
			remote:FireClient(player)
			wait(3)
			rp.CFrame = cPosition
			rp2.CFrame = rPosition
		end
	end
end)

but it still teleports 1by 1 then I tried putting it after the end like this

local rp =character.HumanoidRootPart
		local rp2 = otherCharacter.HumanoidRootPart

		local distance = 100
		local backdistance = 1000
		if (player.Character.HumanoidRootPart.Position - otherCharacter.HumanoidRootPart.Position).Magnitude <= distance then
			local rPosition = rp2.CFrame
			local cPosition = rp.CFrame
			otherCharacter:SetPrimaryPartCFrame(Tpto.CFrame*CFrame.new(math.random(1,20),10,math.random(1,20)))
			character:SetPrimaryPartCFrame(userdest.CFrame*CFrame.new(0,10,0))
			character.PrimaryPart.Orientation = character.PrimaryPart.Orientation + Vector3.new(0,270,0)
			if (player.Character.HumanoidRootPart.Position - otherCharacter.HumanoidRootPart.Position).Magnitude <= backdistance then				
			end
			
			remote:FireClient(player)
		end
	end
	wait(3)
	rp.CFrame = cPosition
	rp2.CFrame = rPosition
end)

But it doesn’t know what rp, rp2, cPosition, and rPosition is. Sorry if I sound really dumb

Quick Tip
Wait() is deprecated, and it can cause some errors.

Use task.Wait() instead.
1.It is more accurate.
2.It is better and more efficient.