I was wondering how I could teleport all players in a car (just like in camping) to a reserved server. I have tried many scripts, but none have been working for me so I decided to make my own post.
here is my code:
local PlayersToTeleport = { workspace.Car1.Seat.Occupant } local PlaceId = 1234567 -- my place ID local code = game:GetService("TeleportService"):ReserveServer(game.PlaceId) game:GetService("TeleportService"):TeleportToPrivateServer(game.PlaceId, code, PlayersToTeleport)
I have 3 cars, with 10 seats each. My goal is to teleport these players to a reserved server (it would be even better if I could set a minimum amount of players and then a countdown until teleport, but I'm just trying to get it to work first.) I put this script into ServerScriptService (there are three scripts, each with a different car.) I have already tried making one script and naming all the cars the same thing, but nothing worked. I have been trying for a while, and any help would be appreciated, thank you so much!
The Occupant
property of VehicleSeat
correlates to the Humanoid
, not the Player
. Since TeleportService’s method TeleportToPrivateServer
requires an array of Players, we have to reformat the table to contain the affiliated Players to the Occupant Humanoids.
This doesn’t take us off rails too far, as we can take the knowledge of the Humanoid
being a child of Character
, and go from there using Players’ GetPlayerFromCharacter
method.
Create a function that will iterate through our Vehicle, and return us a table of all Seated Players with the information we just learned:
local TeleportService = game:GetService("TeleportService") local Reserve = TeleportService:ReserveServer(game.PlaceId) local Players = game:GetService("Players") local Car = workspace.Car1 local function SeatedPlayersThreshold(Vehicle, PotentialMinimum) local AllSeats = {} do for Seat,_ in pairs(Vehicle:GetDescendants()) do if (Vehicle[Seat]:IsA("VehicleSeat")) then AllSeats[Seat] = Seat end end end if not (#AllSeats >= (PotentialMinimum and PotentialMinimum or 1) then return end return PotentialMinimum or #AllSeats end local function GetSeatedPlayersFrom(Vehicle) if not (Vehicle) then return end local SeatedPlayers = {} for _,Seat in pairs(Vehicle:GetDescendants()) do if (Seat:IsA("VehicleSeat")) then local PlayerInSeat = Players:GetPlayerFromCharacter(Seat.Occupant.Parent) if (Players[PlayerInSeat]) then table.insert(SeatedPlayers, PlayerInSeat) end end end if (#SeatedPlayers < SeatedPlayersThreshold(Vehicle)) then return end return SeatedPlayers end TeleportService:TeleportToPrivateServer(game.PlaceId, Reserve, GetSeatedPlayersFrom(Car))
Remember to accept this answer if it helps!
If you want to teleport all players sitting in a car, you should iterate over all the car seats and add the players to an array. Right now you’re only adding the player that’s sitting in car1’s seat. For this you can use a for loop:
local players = {} for i,v in ipairs(game.Workspace:GetDescendants()) do If v:IsA("VehicleSeat") then players[#players + 1] = v.Occupant end end