I want all players who are teleported to the place from the same server to be teleported to the same server as the other players. The problem is the players will be teleported at different times, so i can just mass teleport everyone. How could I achieve this?
local destinationPlaceId = 123456789
local playersToPort = {}
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
table.insert(playersToPort, player)
end)
end)
--however you wish to put together a playersToPort list (table)
for _, player in pairs(playersToPort) do
pcall(function()
game:GetService("TeleportService"):Teleport(destinationPlaceId, player)
end) --task.wait(0.33)
end
Don’t really need the pcall here… I use it, seems to fit the given task to me.
Basically just a for loop using a table. Some may log out !?! …pcall is a bit higher priority and can fail yet keep going. So that covers that.
There was some reason I didn’t use that. I forget now.
If say; a few logged out would that over all fail?
Maybe I was thinking it may… (old script) never really game tested.
Sure looks good…
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local PLACE_ID = 0 -- replace
local playerList = Players:GetPlayers()
local success, result = pcall(function()
return TeleportService:TeleportPartyAsync(PLACE_ID, playerList)
end)
if success then
local jobId = result
print("Players teleported to", jobId)
else
warn(result)
end
I don’t think it would fail, because by the time you call the function it’s already too late for someone to leave. Even if you get the list of players first and wait for a bit the player would be nil and deleted from the table, and if the player isn’t nil I think they would still have the internal things the source code needs to actually “teleport” the player. As well as looking at the list of possible errors, there is none for “player left” or something, it would only error if you add a non-player to the list. However I did notice that TeleportPartyAsync is deprecated in favor of TeleportAsync
This was why… One was deprecated and If a player list (table) is constructed, TeleportAsync will fail if it includes players who are no longer valid or connected. It requires all entries to be active Player instances. If even one is invalid, the entire teleport attempt will fail. You would have to ensure the list only contains currently connected players before calling it.
My post (again not really tested under load) is avoiding any log out error with one at a time quickly using pcall to it keep going. Seemed safer to me at the time… Also;
Group Teleport Limitations
Groups of players can only be teleported within a single experience.
wouldnt this teleport everyone at once? I need the script to teleport one person at a time, but make sure they all go to the same server
I wrote this up:
code=game:GetService("TeleportService"):ReserveServer(tostring(game.JobId)..tostring(group))
game:GetService("TeleportService"):TeleportToPrivateServer(112794835310439,code,{game.Players:GetPlayerFromCharacter(part.Parent)},nil,nil,game.StarterGui.LoadingScreen)
Im sure its not the most efficient but would this code work?