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

When player added, sit in unoccupied seat from a list of cars?

Asked by
Zripple 18
3 years ago
Edited 3 years ago

Ok so. I'm mind boggled right now so any help would be fantastic :)

I'm making it so players spawn into the game seated in a car. For this test game, there are 4 cars and let's say there are 4 players that are going to join. I want to make it so when a player joins it finds a car that isn't occupied and places that player in that car. If the car is occupied, it goes through the list of cars and finds one that isn't.

Here is the first script I tried:

local Players = game:GetService("Players")
local seat = script.Parent
local cars = workspace.cars:GetDescendants()

Players.PlayerAdded:Connect(function(player)
    for _, v in pairs(cars) do
        if v:IsA("VehicleSeat") then 
            if seat.Occupant == nil then
                wait(2.5)
                seat:Sit(player.Character.Humanoid)
            end
        end
    end
end)

This script worked for the first player that joined and placed them in the car. But sadly other places spawned normally at the world spawn.

Here is the second script I tried:

local Players = game:GetService("Players")
local seat = script.Parent
local cars = workspace.cars:GetDescendants()

Players.PlayerAdded:Connect(function(player)
    for _, v in pairs(cars) do
        if v:IsA("VehicleSeat") then 
            if seat.Occupant == nil then
                wait(2.5)
                seat:Sit(player.Character.Humanoid)
            elseif seat.Occupant ~= nil then
                for y, p in pairs(cars) do
                    if p:IsA("VehicleSeat") then
                        if seat.Occupant == nil then
                            wait(2.5)
                            seat:Sit(player.Character.Humanoid)
                        end
                    end
                end
            end
        end
    end
end)

This script did the exact same thing as the firsst one. I lost all brain cells when thinking this would make a difference. Tried to make it so if it finds that the seat is occupied then it rolls through the list again until it finds one that isn't and sits the player in it.

Picture of car folder and contents: click here

Also note:

I would rather this one main script be placed once in the 'cars' folder rather than placed in every 'VehicleSeat'.

local seat = script.Parent

At the moment, it's looking to the parent which is the seat.

If you have any questions, please ask. :)

1 answer

Log in to vote
0
Answered by 3 years ago

Ima attempt to solve it and put it in one script, here goes nothing:

local Players = game:GetService("Players")
local cars = workspace.cars:GetDescendants()


Player.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Wait()

  for _, v in pairs(cars) do
if v:IsA("VehicleSeat") then 
local seat = v
if seat.Occupant == nil then

wait(2.5)
seat:Sit(plr.Character:WaitForChild("Humanoid"))
break-- to stop the loop from moving on since you already found an empty seat






end
end
end
end)

error I noticed:
You didn't wait for the player's character to be added
You forgot to break the loop when it finds an empty seat

I know I am 6 days late but I hope I helped

Ad

Answer this question