How do I MoveTo every player at the same time?

I’m trying to make every player in a certain team teleport to a position, and then start walking in unison. The problem is that it only teleports a single player, and then doesn’t teleport the next player until the first player is done walking the entire length. My plan is obviously to have them all do it at once. I’ve looked for a long time on the DevForums but I haven’t seen anything about this issue so I’m choosing to ask for help. Here is the section of the script:

local function CompSpawned(player)
		Comp:Clone().Parent = workspace.CurrentComp	-- Here I'm just spawning a minigame 
		spawn (function() 
for i,v in pairs(teams[teamone]:GetPlayers()) do
 if v.Character and v.Character:FindFirstChild("HumanoidRootPart")then
 	v.Character.HumanoidRootPart.CFrame = SelectedPartOne.CFrame
wait(1)
	v.Character.Humanoid:MoveTo(Vector3.new(-163.8, 38.2, 2271.5))
    v.Character.Humanoid.MoveToFinished:Wait()
    v.Character.Humanoid:MoveTo(Vector3.new(-157.2, 38.2, 2318.8))
    v.Character.Humanoid.MoveToFinished:Wait()
    v.Character.Humanoid:MoveTo(Vector3.new(-130.6, 38.2, 2388.8))
    v.Character.Humanoid.MoveToFinished:Wait()
    v.Character.Humanoid:MoveTo(Vector3.new(-84, 38.2, 2438.7))
    v.Character.Humanoid.MoveToFinished:Wait()
    v.Character.Humanoid:MoveTo(Vector3.new(-6.7, 38.2, 2468.9))
    v.Character.Humanoid.MoveToFinished:Wait() 
    v.Character.Humanoid:MoveTo(Vector3.new(-10.034, 40.739, 2334.671))    
    v.Character.Humanoid.MoveToFinished:Wait() 	
		end
	end
end)

If you’re using for i,v, it’s going to teleport all your players for each action. It’s going to do 1 player by 1. A solution to fix this would be to have for i,v’s a little bit everywhere. This might make your script long, but tbh I have no other solutions.

for i,v in pairs(teams[teamone]:GetPlayers()) do
	if v.Character and v.Character:FindFirstChild("HumanoidRootPart")then
 		v.Character.HumanoidRootPart.CFrame = SelectedPartOne.CFrame
		wait(1)
		v.Character.Humanoid:MoveTo(Vector3.new(-163.8, 38.2, 2271.5))
		v.Character.Humanoid.MoveToFinished:Wait()
	end
end
for i,v in pairs(teams[teamone]:GetPlayers()) do
  		v.Character.Humanoid:MoveTo(Vector3.new(-157.2, 38.2, 2318.8))
  		v.Character.Humanoid.MoveToFinished:Wait()
end
for i,v in pairs(teams[teamone]:GetPlayers()) do
    		v.Character.Humanoid:MoveTo(Vector3.new(-130.6, 38.2, 2388.8))
   		v.Character.Humanoid.MoveToFinished:Wait()
end
for i,v in pairs(teams[teamone]:GetPlayers()) do
 		v.Character.Humanoid:MoveTo(Vector3.new(-84, 38.2, 2438.7))
  		v.Character.Humanoid.MoveToFinished:Wait()
end
for i,v in pairs(teams[teamone]:GetPlayers()) do
  		v.Character.Humanoid:MoveTo(Vector3.new(-6.7, 38.2, 2468.9))
   		v.Character.Humanoid.MoveToFinished:Wait() 
end
for i,v in pairs(teams[teamone]:GetPlayers()) do
   		v.Character.Humanoid:MoveTo(Vector3.new(-10.034, 40.739, 2334.671))    
   		v.Character.Humanoid.MoveToFinished:Wait() 	
end

I tried doing this and it didn’t solve it. It teleported the first player and made him walk to the first position, and after that another player was teleported with the same thing happening. So this will still only make 1 player move at once. If it helps this is on a Script not a LocalScript.

Then I’d recommend to remove all the v.Character.Humanoid.MoveToFinished:Wait(), as it waits for the action to be over. But you would need to add a wait(n)after the for i,v’s.

Just wait(n)? I haven’t set “n” as anything so what would I need to set it as?

No, I meant n as a number. I can’t really assume the number myself, because I don’t know how long the Humanoid:MoveTo will last for. I suggest you figure it out yourself. Hope this can help!!!

I tried using wait and the problem persisted, however I didn’t add a wait after every line and so once all the waits were over they all moved at the same time meaning that the waits are causing this, I don’t know how to make a solution yet but that helps to know for sure!

1 Like

Ah, my apologies for wasting your time. I hope you’ll be able to find a solution as soon as possible. :two_hearts:

1 Like

Have you tried coroutines in the for pairs loop?

spawn already has a builtin wait(), move every player as uniformly as possible using coroutines instead.

for _, player in ipairs(Players:GetPlayers()) do -- for every player, 
    -- ipairs guarantees order and should be used with fully numerically indexed tables
    coroutine.wrap(function() 
    -- create a new thread for every player, do your teleportation stuff here
    end)() -- the extra parentheses are needed to execute the function
end

Generic for loops to be more precise, and that’ll yield for almost the same amount of time- you can just do it all in one block.

6 Likes

This worked! Thanks a lot for the answer :grinning:. The reason why I used the spawn function is because this line of code actually runs twice at the same time (with slight variation as it makes 2 teams walk at once), I’ll include a picture of it so you see what I mean. And so you can see any more mistakes if I’ve made some.

2 Likes

You can shorten your code.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnComp = ReplicatedStorage.SpawnCoconutComp
local Comp = ReplicatedStorage.Comps.CoconutComp -- wfc not necessary
local teams = game:GetService("Teams")

local DockspawnRed = workspace.CompIsland.KalabawDockTwo.Spawns:GetChildren()
local DockspawnYellow = workspace.CompIsland.TandangDockTwo.Spawns:GetChildren()
local SelectedPartOne = DockspawnRed[math.random(#DockspawnRed)] -- 1 not necessary
local SelectedPartTwo = DockspawnYellow[math.random(#DockspawnYellow)]

local positions = 

{
	["Host"] =    { {-163.8, 38.2, 2271.5}, {-157.2, 38.2, 2318.8}, {-130.6, 38.2, 2388.8}, {-84, 38.2, 2438.7}, {-6.7, 38.2, 2468.9}, {-10.034, 40.739, 2334.671} };
	["Tandang"] = { {205.5,  38.2, 2330.1}, {170.5, 38.2, 2401.1},  {109.5, 38.2, 2452.1},  {45.5, 38.2, 2471.1}, {50.965, 40.739, 2334.671} };
}

local function CompSpawned(player)
      local Host = teams["Host"]
      local Tandang = teams["Tandang"]

      teams = {{Host, SelectedPartOne}, {Tandang, SelectedPartTwo}}

	  Comp:Clone().Parent = workspace.CurrentComp
	  for _, team in ipairs(teams) do
	   	 for _, v in ipairs(team[1]:GetPlayers()) do
		     coroutine.wrap(function() 
			       if v.Character and v.Character:FindFirstChild("HumanoidRootPart")then
 	               v.Character.HumanoidRootPart.CFrame = team[2].CFrame
				   wait(1) -- still don't know why you included this 
				      for index = 1, #positions[tostring(team[1])] do        
						v.Character.Humanoid:MoveTo(Vector3.new(unpack(positions[tostring(team[1])][index])))
					    v.Character.Humanoid.MoveToFinished:Wait()
				      end
		           end
	          end)()
           end
	   end	
   end


SpawnComp.OnServerEvent:Connect(CompSpawned)
note
  • Don’t call ReplicatedStorage:WaitForChild(“str”) ever on the server, unless there is explicit guaranteed delay from your side, it’s redundant.

  • math.random takes two arguments, but just x provided works as math.random(1, x) = math.random(x)

There could be errors, but there shouldn’t be anything too hard to fix.

1 Like

Thanks a lot! :laughing: You’ve been very kind to me!

EDIT: To clarify the “Wait(1)” that’s because I don’t want them to start moving right after being teleported because that looks a bit strange if you ask me.

1 Like