So I have this teleport for a lobby system but I am unsure if I handled the teleports correctly as I have it setup for when players are in the server after they are teleported and also for if they leave the game that they are teleported to but don’t know if I have things correct for if a players teleport fails.
This is in the lobby that the players will be teleported from
local lobbyRE = replicatedStorage.Client.lobbyRE --RemoteEvent
local playersInLobby = {} --table that has the names of players that enter the lobby
function teleportLobby()
local ID = 14025001954
local reservedServer = teleportService:ReserveServer(ID)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ReservedServerAccessCode = reservedServer
local success, result = pcall(teleportService.TeleportAsync, teleportService, ID, playersInLobby, teleportOptions)
if not success then
lobbyRE:FireClient("teleportFail") --displays a failed teleport GUI
warn("Teleport Failed")
return
end
teleportOptions:SetTeleportData({
["totalPlayersInLobby"] = #playersInLobby, --sends the amount of players to be teleported not taking into account players whos teleport failed
})
print("Teleporting players from lobby".. lobby.Name)
end
And this is in the server that players are teleported to
local userQueueConnectionType = replicatedStorage:WaitForChild("userQueueConnectionType") --RemoteEvent
local totalPlayers
local playersInGame = script.playersInGame --IntValue
playersInGame.Value = 0
game.Players.PlayerRemoving:Connect(function(leftPlayer) -- this is what happens for if a player leaves the game after they are loaded in but i need this to also happen if the player fails to teleport
userQueueConnectionType:FireAllClients(leftPlayer, "left")
playersInGame -= 1
if totalPlayers ~= "error" then
totalPlayers -= 1
end
end)
players.PlayerAdded:Connect(function(player)
local joinData = player:GetJoinData()
local lobbyData = joinData.TeleportData
local success, result = pcall(function()
totalPlayers = lobbyData.totalPlayersInLobby -- sets the value to the amount of players that were initially teleported still not taking into account failed teleports
end)
if not success then
totalPlayers = "error" --for when in studio
end
end)