Im trying to teleport a group to a private server from the lobby. (Similar as Tower Defence Simulator, or Dungeon Quest,etc)
Currently, I’m teleporting the players fine but I’m trying to find a way for the game to start after all the players that were teleported load.
My solution to this is to send a table from the server containing all the player’s names but you cannot do that (As far as I know).
My Second solution to this is solution 1 but on the client. This would work but it may be a potential risk factor for exploitation.
Is there any other solutions I can do, I want to ensure that all the players teleported (will time out). But all the players who did teleport are loaded in completely.
If you’re teleporting players using TeleportPartyAsync, you can use the GetJoinData function of the Player whenever a player joins the game. It returns an immutable table and used on the server, so it is secure, and one of the entries is Members which contains UserIds of all of the players that are teleported alongside the player
local Players = game:GetService("Players")
local Player = Players.PlayerAdded:Wait()
local members = Player:GetJoinData().Members
local ready = false
local joined = {}
function PlayerAdded(player)
joined[#joined + 1] = player.UserId
if #joined == #members then
ready = true
end
end
-- Adding the current player, or any players that happened to join before the event has been made, to the table
for _, v in ipairs(Players:GetPlayers()) do
PlayerAdded(v)
end
-- Adding players who joined to the table
local con = Players.PlayerAdded:Connect(function(player)
PlayerAdded(player)
end)
-- Giving time for players to join
for i = 30, 0, -1 do
if ready then
break
end
wait(1)
end
-- disconnecting the event
con:Disconnect()
con = nil
-- All players have joined now (or time has passed for players to join)
-- Proceed with the game
It would teleport to a public server that players could join, but using teleport data would help. In the source game (place of origin), you’ll make a table which contains a list of players (their userIds) in a table and pass it as the teleportData in the TeleportToPrivateServer function and just grab the teleport data from the first player that joined in the target place. A remote event is needed, where you will send a request to the first client to get the data and return it to the server:
-- Client
RE.OnClientEvent:Connect(function()
local data = TeleportService:GetLocalPlayerTeleportData()
RE:FireServer(data)
end)
-- Server
local Players = game:GetService("Players")
local joined = {}
local ready = false
local members
RE.OnServerEvent:Connect(function(player, data)
members = data
end)
local Player = Players.PlayerAdded:Wait()
RE:FireClient(player) -- obtain teleport data, which is the members
while (not members) do wait() end -- Waiting to get the data
function PlayerAdded(player)
joined[#joined + 1] = player.UserId -- Keep track of players that joined
end
for _, player in ipairs(Players:GetPlayers()) do
PlayerAdded(player) -- Account for players that have already joined, including the first player
end
local con = Players.PlayerAdded:Connect(PlayerAdded)
for i = 30, 0, -1 do
if #joined == #members then
break
end
wait(1)
end
con:Disconnect()
con = nil
-- All players have joined (or 30 seconds has elapsed)
-- Proceed with the game