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

How to teleport players to their cars?

Asked by 3 years ago

Hi! I wanted to make a race game but I don't know how to teleport players to their cars. Here's my local script located in starter player scripts(SPS) :

local car1 = workspace.minigames.carrace.cars["Formula F1"].DriveSeat
local car2 = workspace.minigames.carrace.cars["Formula F2"].DriveSeat
local car3 = workspace.minigames.carrace.cars["Formula F3"].DriveSeat
local car4 = workspace.minigames.carrace.cars["Formula F4"].DriveSeat
local car5 = workspace.minigames.carrace.cars["Formula F5"].DriveSeat
local car6 = workspace.minigames.carrace.cars["Formula F6"].DriveSeat
local car7 = workspace.minigames.carrace.cars["Formula F7"].DriveSeat
local car8 = workspace.minigames.carrace.cars["Formula F8"].DriveSeat

local playerModel = game.Players.LocalPlayer.Character
local humanoid = playerModel:WaitForChild("Humanoid")
        if car1.Occupant == nil and not humanoid.Seated then
            print("car1")
            car1.Value = true
            game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = car1.Parent.CFrame 
        end

        if car2.Occupant == nil and not humanoid.Seated then
            print("car2")
            car2.Value = true
            game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = car2.Parent.CFrame 
        end
        if car3.Occupant == nil and not humanoid.Seated then
            print("car3")
            car3.Value = true
            game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = car3.Parent.CFrame 
        end
        if car4.Occupant == nil and not humanoid.Seated then
            print("car4")
            car4.Value = true
            game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = car4.Parent.CFrame 
        end
        if car5.Occupant == nil and not humanoid.Seated then
            print("car5")
            car5.Value = true
            game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = car5.Parent.CFrame 
        end
        if car6.Occupant == nil and not humanoid.Seated then
            print("car6")
            car6.Value = true
            game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = car6.Parent.CFrame 
        end
        if car7.Occupant == nil and not humanoid.Seated then
            print("car7")
            car7.Value = true
            game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = car7.Parent.CFrame 
        end
        if car8.Occupant == nil and not humanoid.Seated then
            print("car8")
            car8.Value = true
            game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = car8.Parent.CFrame 
        end

the problem is that nothing works at all. (nothing prints or teleports) I thought the best way would be to check that the player is sitting and that the seat it is sitting on is occupied. Thanks for every answer :)

0
The problem is that it's in a local script. Ieowyyn 69 — 3y

1 answer

Log in to vote
1
Answered by
Ieowyyn 69
3 years ago

The problem is that it's in a local script. If you want to get a player in a server script, use PlayerAdded and CharacterAdded. Also, there's a much easier way instead of constantly clone and edit the numbers of each statement.

local car1 = workspace.minigames.carrace.cars["Formula F1"].DriveSeat
local car2 = workspace.minigames.carrace.cars["Formula F2"].DriveSeat
local car3 = workspace.minigames.carrace.cars["Formula F3"].DriveSeat
local car4 = workspace.minigames.carrace.cars["Formula F4"].DriveSeat
local car5 = workspace.minigames.carrace.cars["Formula F5"].DriveSeat
local car6 = workspace.minigames.carrace.cars["Formula F6"].DriveSeat
local car7 = workspace.minigames.carrace.cars["Formula F7"].DriveSeat
local car8 = workspace.minigames.carrace.cars["Formula F8"].DriveSeat

local cars = {car1,car2,car3,car4,car5,car6,car7,car8}

game.Players.PlayerAdded:Connect(function(player) -- once a player joins, the script starts and creates the "player" variable
    player.CharacterAdded:Connect(function(character) -- then when the character is added, it creates another variable
        for _,car in pairs(cars) do -- start the loop
            if car.Occupant == nil and not character.Humanoid.Seated then
                print(car)
                car.Value = true
                character.HumanoidRootPart.CFrame = car.Parent.CFrame
            end
        end
    end)
end)

Have a great day or night!

0
It worked, Thank you! :) IzaXD123456 21 — 3y
Ad

Answer this question