How to prevent players from being teleported to the same place?

You know that game Camping? There’s 3 cars you get in one and when they fill up, they drive away and all the players inside get teleported to another place.

But what if 2 cars drove away at the same time and both of them were only half full, they would all be teleported to the same place. How would I make a script to prevent 2 lobbys from teleporting to the same place?

1 Like

Use ReserveServer it’s like a private server / vip server(old name) except it’s created by the developer, so you can manage it

Also see

What I’m assuming Camping uses is TeleportService:ReserveServer() and TeleportService:TeleportToPrivateServer() to teleport every player to a new “private” server when the bus drives away. Look here for more information on both of these functions:

ReserveServer()
TeleportToPrivateServer()

Could you maybe write an example script? I don’t understand

Well watch the post

Ex Code :

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")
 
local code = TS:ReserveServer(game.PlaceId) -- Returns a code for the gameID
local players = Players:GetPlayers() -- Get a list of all players
 
TS:TeleportToPrivateServer(game.PlaceId,code,players) -- Actually teleport the players

what you will have to do is quite similar, you just need to replace players with a list that contains the object players of those in the vehicle

1 Like

I’ve tried to make a script but its not working. This is the error I get:
Unable to cast value to Objects

ServerScript:

local teleportService = game:GetService("TeleportService")
local code = teleportService:ReserveServer(game.PlaceId)

part.Touched:Connect(function()
    game.ReplicatedStorage.GetPlayer:FireAllClients()
end)

game.ReplicatedStorage.ReturnPlayer.OnServerEvent:Connect(function(player)
    teleportService:TeleportToPrivateServer(5189057763, code, player) -- this is the line with the error (5189057763 is the id of the place btw)
end)

Client Script:

local player = game.Players.LocalPlayer
local teleportService = game:GetService("TeleportService")
local teleportGui = player:WaitForChild("PlayerGui"):WaitForChild("TeleportGui")

game.ReplicatedStorage.GetPlayer.OnClientEvent:Connect(function()
    teleportService:SetTeleportGui(teleportGui)
    game.ReplicatedStorage.ReturnPlayer:FireServer(player)
end)

Its a pretty sloppy script atm Im just testing

First, if you are going to do something like this where you have two RemoteEvents called GetPlayer and ReturnPlayer use a RemoteFunction instead. Second, TeleportService:TeleportToPrivateServer() Takes 3 required arguments.

  1. The PlaceId to teleport to.
  2. The ReservedServer code.
  3. A table of players.

Use this instead:

local teleportService = game:GetService("TeleportService")
local code = teleportService:ReserveServer(game.PlaceId)

part.Touched:Connect(function()
    game.ReplicatedStorage.GetPlayer:FireAllClients()
end)

game.ReplicatedStorage.ReturnPlayer.OnServerEvent:Connect(function(player)
    teleportService:TeleportToPrivateServer(5189057763, code, {player}) -- Doing {player} creates a new table and puts the player object into it.
end)
1 Like

Teleport failed because access code for reserve server is not valid

2021-01-02 (3)

local teleportService = game:GetService("TeleportService")

local code = teleportService:ReserveServer(game.PlaceId)
teleportService:TeleportToPrivateServer(5189057763, code, {player})

Edit: Oh Im dumb, need to replace the game.PlaceId with 5189057763 lol

local teleportService = game:GetService("TeleportService")

local code = teleportService:ReserveServer(5189057763)
teleportService:TeleportToPrivateServer(5189057763, code, {player})

This is because you use “5189057763” for the teleport function, but you used game.PlaceId for the ReserveServer function.

Change it to this and see if it works:


local teleportService = game:GetService("TeleportService")

local code = teleportService:ReserveServer(5189057763)
teleportService:TeleportToPrivateServer(5189057763, code, {player})
1 Like

Thanks for your help man, appreciate it. All working now. :pray: