Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to teleport all players in seats to reserved server?

Asked by
mewtify 26
4 years ago

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!

0
If my answer helped, don’t forget to accept! Ziffixture 6913 — 4y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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!

0
thanks so much, do you know how I would make the countdown to start teleporting start once a certain amount of players enter the vehicle? also, would I place this script in the car or ServerScriptService? mewtify 26 — 4y
0
I changed my Script, it should now only let people teleport if all Seats or a specified amount of Seats are populated, going about a countdown, which is a lot simpler, I’ll have to leave it to you! Ziffixture 6913 — 4y
0
thanks so much for all the help so far! after putting some other things in, unfortunately I can't get it to work :(. I was wondering where the countdown is and how I could edit it since I didn't see anything looking over the script. I'm also getting an error with the Reserve (second) line, even after placing my PlaceId before. thanks so much again and I am so sorry for all the inconveniences! mewtify 26 — 4y
0
Please contact my discord @Ziffixture#0152 Ziffixture 6913 — 4y
0
I send a friend request, I should be just mew#4730 mewtify 26 — 4y
Ad
Log in to vote
-1
Answered by 4 years ago
Edited by Ziffixture 4 years ago

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
0
Feahren, it doesn't matter if you use ipairs or pairs in this situation. Also, game.workspace does work. Unreasonably downvoting and editing my post. Useless joshmatt2244 28 — 4y
0
Oh! Apologies, I wasn’t changing these to fix your code, I just edited it as a form to improve some methods. I originally intended to revert my modifications but missed these things. I downvoted because the code doesn’t work Ziffixture 6913 — 4y

Answer this question