Best method of getting teams from a teleport?

Hello, I am currently making a game with a lobby that allows players to teleport to different places with different game modes. Some of these game modes have teams that players can form before they teleport, and upon arrival I would like to be able to determine these teams.

My issue was determining the best method to do so. The most intuitive way I can see is using DataStoreService, but I wanted to avoid using them if possible. Could someone tell me the best way to go about this?

(Current strategy)

Somewhere in the teleport code in the lobby:

local serverAccessCode = TeleportService:ReserverServer(placeId)
local serverAccessKey = "server$" .. serverAccessCode
local success = pcall(dataStore.SetAsync, dataStore, serverAccessKey, serverTeleportData)
if not success then
  -- some error handler
end

Somewhere in the new server’s initialization code:

local serverAccessKey = "server$" .. game.JobId
local success, result = pcall(dataStore.GetAsync, dataStore, serverAccessKey)
if success then
  pcall(dataStore.RemoveAsync, dataStore, serverAccessKey) -- don't really know what to do if it fails
  -- continue game stuff (result now holds the team information)
else
  -- error handling logic
end

TeleportService has just what you need!

The function TeleportService:TeleportToPrivateServer() accepts several arguments, one of which is teleportData.

In this case, you could provide a string.

TeleportService:TeleportToPrivateServer(yourGameId, serverAccessKey, playerInstance, _, teamName)

To retrieve the teleportData, you may use the following code:

game.Players.PlayerAdded:Connect(function(Plr)
    local JoinData = Plr:GetJoinData()
    if JoinData.TeleportData then
        --do stuff
    else 
        --catch missing teleport data
    end
end)

Do keep in mind, however, that while players cannot modify their teleportData, they can replace it with data that was previously sent. Datastores are a safer alternative.

A way to secure this system would be to have each player carry a table containing every player’s teamName. Then, have the server check if all the tables that the clients have sent are the same. If there are differences, use the most common array (the one that shows up most often). This means that in order to be able to cheat and change teams, 51% of the players must be exploiting, which is highly unlikely.

You can read more on this here:

3 Likes