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?
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:
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
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)
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.
The PlaceId to teleport to.
The ReservedServer code.
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)