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

Why won't my script teleport players in a seat?

Asked by
uhjos_h 19
3 years ago
Edited 2 years ago
local seats = workspace.Seat

local teleportService = game:GetService("TeleportService")

local num = 0

local playersInSeats = {}

local countdown = false


local function countDown()

    if not countdown then 

        countdown = true

        print("Start countdown")
        playersInSeats = {}

        for i = 5,1,-1 do

            if num < 1 then break end -- if everyone gets off, reset timer
            if num == #seats:GetChildren() then break end -- if seats are full, teleport
            wait(1)

        end

        if num >= 4 then

            print("Start game")

            local serverCode = teleportService:ReserveServer(6597645798)

            for _,seat in pairs(seats:GetChildren()) do

                if seat.Occupant then

                    local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)

                    table.insert(playersInSeats,plr)

                end

            end


            teleportService:TeleportToPrivateServer(6597645798,serverCode,playersInSeats)

            countdown = false

        else

            countdown = false

        end

    end

end

for _,seat in pairs(seats:GetChildren()) do

    if seat:IsA("Seat") then

        seat:GetPropertyChangedSignal("Occupant"):connect(function()

            if seat.Occupant ~= nil then

                num = num + 1
                countDown()

            else

                num = num - 1

            end

        end)

    end

end

I decided to redo my code from my old code, because this is far more reliable. It just won't teleport at all.

This is going to a Reserved Server within the starting place.

Any help would be deeply appreciated, once I get this first section done - I can finally actually start the real development.

0
please use Connect instead of connect as it is deprecated RazzyPlayz 497 — 3y
0
Thanks for letting me know uhjos_h 19 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Hello.

This may not be the most efficient way to code what you are trying, but rather a functional version of your code.

Your countdown has been coded incorrectly, and will stop at 5 any time it begins, your script will also attempt to teleport the players before the seats are full. The teleport will work however it will work incorrectly.

The countdown counts down to 0 every time a seat is changed, and if the seats arent full then nothing will happen, but if they are full, then after the 5 second countdown the game will start. Becareful for trolls however, as somebody could always keep leaving the seats anytime the game is about to start.

Try this instead:

local Seats = workspace.Seats
local Number = 0

local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")

local PlayersSitting = {}
local Countdown = false

local function CountdownTeleport()
    if not (Countdown) then
        Countdown = true

        PlayersSitting = {}

        for CountdownTimer = 5, 0, -1 do
            print(CountdownTimer)
            if (Number < 1) then
                break
            else
                if (CountdownTimer == 0) then
                    if (Number == #Seats:GetChildren()) then
                        break
                    end
                end
            end
            wait(1)
        end
    end

    if (Number >= 4) then

        local ReservedServer = TeleportService:ReserveServer(6597645798)

        for _, Seat in pairs(Seats:GetChildren()) do
            if (Seat.Occupant) then
                local Player = Players:GetPlayerFromCharacter(Seat.Occupant.Parent)

                table.insert(PlayersSitting, Player)
            end
        end
        TeleportService:TeleportToPrivateServer(6597645798, ReservedServer, PlayersSitting)
        Countdown = false
    else
        Countdown = false
    end
end

for _, Seat in pairs(Seats:GetChildren()) do
    if (Seat:IsA("Seat")) then
        local function DetectSeatChange()
            if (Seat.Occupant ~= nil) then
                Number += 1
                CountdownTeleport()
            else
                Number -= 1
            end
        end
        Seat:GetPropertyChangedSignal("Occupant"):Connect(DetectSeatChange)
    end
end

Make sure you have a folder in workspace named Seats with the seats inside of it.

Hope this helps

Ad

Answer this question