How to use something like in pairs at the same time

How do I make something like in pairs to do something at the same time instead of it waiting for the first one to be finished and only then doing it for the second time (kinda hard to explain so I attached a video)

If you want to keep it simple :
task.spawn
coroutine.wrap

can you give an example on how to use them

Looks like it uses TeleportService.
Are you calling Teleport or TeleportAsync?

I use something like player.character:MoveTo(game.Workspace.Teleport.Position)

so just wait once and then update all cubes?

no no the cube isn’t the problem it just has the proximity prompt in it.
the problem is that the script uses “in pairs” to do the white UI fade in animation and teleport all players, but since the animation uses “wait(0.2)”, it does the players one by one instead of all of them at the same time

How did you even manage to write this? :skull: you supposed to teleport all players in SS and play animation through client.

Its only on roblox you may see absurd like this for server to tell client every time to update property of UI :skull:

So I ended up with this:

--!strict
-- server script, child of "start" part

local start = script.Parent
local destination = workspace:WaitForChild("end")
local moving_now = 0
local max_wait = 10

start.Touched:Connect(function(other_part : BasePart)
	if 0 ~= moving_now then
		-- ignore this touch since moving is in progress
		return
	end
	local player = game:GetService("Players"):GetPlayerFromCharacter(other_part.Parent)
	if not player then
		-- not a player
		return
	end
	
	-- move all players: TODO: use only required players
	local players = game:GetService("Players"):GetPlayers()
	for _, player in players do
		local character = player.Character
		if not character then
			continue
		end
		moving_now += 1
		task.spawn(function()
			local humanoid : Humanoid = character:FindFirstChild("Humanoid")
			humanoid:MoveTo(destination.Position)
			humanoid.MoveToFinished:Wait()
			moving_now -= 1
		end)
	end
	
	-- wait for all players to move or timeout	
	local start_time = tick()
	while tick() - start_time < max_wait and 0 ~= moving_now do
		task.wait(1)
	end
	
	if 0 ~= moving_now then
		print("stopped by timeout")
	end
	
	moving_now = 0
end)

I tested this locally in roblox studio in “Server & Clients” debug mode: both players do start moving to destination at the same time when one of players touches “start” part.
The key point is task.spawn mentioned before: each function kind of thread that starts movement and waits for its end.
moving_now prevents double entrance.
max_wait handles cases when player moves character during MoveTo so the movements stops and thus MoveToFinished never triggers. It’s not the only way to handle this, you can have your own logic.

I know how to use task.spawn, so I can give you an example on that:

for i, v in instance:GetChildren() do
   task.spawn(function()
      if v:IsA("BasePart") then
         task.wait(1)
         part.Transparency = 1
         task.wait(1)
         part.Transparency = 0
      end
   end
end

That’ll run independently and won’t stop the rest of the function from continuing. Put some parts in a folder and replace instance with that folder to test it.

here is how task.spawn() works:

Normal

local function test()
print("Start")
task.wait(5)
print("End")
end)

for i  = 1, 5 do
test()
end

task.spawn():

local function test()
print("Start")
task.wait(5)
print("End")
end)

for i  = 1, 5 do
task.spawn(function()  --OR task spawn(test)
test()
end)
end

didnt see this mb


Sorry for the late reply, Usually I get a notification when I get a response, but I didn’t get it.

So your code is something like this :

for _,player in game:GetService("Players"):GetPlayers() do
	print("Teleporting takes 6 seconds")
	task.wait(6)
	print("Teleported")
end 

put everything in the for loop inside of task.spawn(function() end), like this :

for _,player in game:GetService("Players"):GetPlayers() do
	task.spawn(function()
		print("Teleporting takes 6 seconds")
		task.wait(6)
		print("Teleported")
	end) 
end