Keeping it simple and clear. How would I teleport a player to the same server that they were in?
If you mean like rejoin
game:GetService("TeleportService"):TeleportToPlaceInstance(game.PlaceId, game.JobId, player)
you would store their previous JobID in memory store or data store and use teleportasync when they rejoin
Did you teleport the player to a different server and would like to teleport them back to the server they were in before? If that’s the case, I think you’d want to use TeleportOptions, more specifically, the part about Teleporting to Specific Servers. You can also pass along data, such as the JobId (basically, the server’s id) and use that to teleport them back.
This stuff should and must be handled on the server, so you should be using RemoteEvents to pass along and request data, as you will be working between the client/server boundary.
To teleport a player with their server’s JobId sent as TeleportData, you can use a script like this.
local TeleportService = game:GetService("TeleportService")
local JobId = game.JobId -- gets the server id, this will be a blank string if ran in studio
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions:SetTeleportData(JobId) -- the JobId of the server you're currently in. Will be passed along when the player joins the new server.
TeleportService:TeleportAsync(placeId, player, teleportOptions)
The TeleportData is only sent via the client, so to send it to the server, you can use a LocalScript and RemoteEvent like this.
local sendJobId = game.ReplicatedStorage.sendJobId -- RemoteEvent in ReplicatedStorage
local teleportData = game:GetService("TeleportService"):GetLocalPlayerTeleportData() -- gets the data that the player arrived with
if teleportData then -- if the player arrived with teleportData
sendJobId:FireServer(teleportData) -- tells the server the JobId of the server which the player got teleported from
end
To have the server save that information for future use, you can use a script like this.
local sendJobId = game.ReplicatedStorage.sendJobId
-- creates a new folder to store all the player's JobIds.
local JobIdFolder = Instance.new("Folder")
JobIdFolder.Parent = game.ServerStorage
JobIdFolder.Name = "JobIdFolder"
sendJobId.OnServerEvent:Connect(function(player, info) -- runs when the client fires the "sendJobId" RemoteEvent
if info then -- if there were arguments provided by the client (prevents the script from erroring)
local PlayerJobId = Instance.new("StringValue")
PlayerJobId.Parent = JobIdFolder
PlayerJobId.Name = player.Name
PlayerJobId.Value = info -- sets the value of the JobId to the JobId provided by the client
end
end)
game:GetService("Players").PlayerRemoving:Connect(function(player) -- remove the data when the player leaves
local PlayerJobId = JobIdFolder:FindFirstChild(player.Name)
if PlayerJobId then
PlayerJobId:Destroy()
end
end)
Now, to teleport the player back to their original server, you can use a script like this.
local TeleportService = game:GetService("TeleportService")
local PlayerJobId = game.ServerStorage:WaitForChild("JobIdFolder"):WaitForChild(player.Name)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ServerInstanceId = PlayerJobId
TeleportService:TeleportAsync(placeId, player, teleportOptions)
I haven’t tested any of this code, so there may be some errors. I’ve also not messed with TeleportService too much, so my apologies if any information I’ve provided is incorrect. If you have any questions, feel free to ask me!