Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make it so that once all 6 seats are filled with players then the bus will start moving?

Ive tried everything and I dont know how to mention when the occupation of the seat is true with a humanoid. This is what I have now, local seat = script.Parent --This is the group that has all 6 seats

seat.Touched:connect(function(object)

local character = game.Players:GetPlayerFromCharacter(object.Parent) and

object.Parent

Here and below is how the bus moves, but i want to trigger it once all 6 seats are filled.

for i = 0,-100,-.01 do

script.Parent.Parent.Parent.Parent.Parent:TranslateBy(Vector3.new(0,0,i))

wait(.01)

end

end)

2 answers

Store your seats in a Folder and use a loop (or changed event for efficiency) to check if the seat has an occupant on it. https://developer.roblox.com/api-reference/property/Seat/Occupant

If the seat has an occupant, it means a Humanoid is sitting on it. This can be an NPC or a Player, you might want to do checking before hand to see if it's a Player or not.

Make a loop, loop through all the seats checking if there is a player, store it in a table, then check if the table length is greater than or equal to 6. OR Have a table, add changed events (preferabley :GetPropertyChangedSignal()), check if occupant is added or removed, add or remove from table, then check if table length is greater than or equal to 6.

More on :GetPropertyChangedSignal() here. More on tables (and table lengths) here. More on checking if the player is an NPC or Player here.

2 comments

0

Or you could just make an int value that holds the amount of players. The table seems to be useless in this case.

0

Ive done the table - local busseat = {workspace.Bus1.Truck.VS3.Body.Seats.Folder.Seat1.Occupant'Humanoid',workspace.Bus1.Truck.VS3.Body.Seats.Folder.Seat2.Occupant'Humanoid',workspace.Bus1.Truck.VS3.Body.Seats.Folder.Seat3.Occupant'Humanoid'} table.insert(busseat,workspace.Bus1.Truck.VS3.Body.Seats.Folder.Seat4.Occupant'Humanoid',workspace.Bus1.Truck.VS3.Body.Seats.Folder.Seat5.Occupant'Humanoid',

What you'd want to do is detect if a player is sitting via their humanoid then add a value each time a player sits and if the player jumps up, then you can remove a value.

You can detect if a player is sitting via;

Player = Something --Get the player's character here
if Player.Humanoid.Sit == true then
-code
end

For more information; https://developer.roblox.com/api-reference/class/Humanoid

Or

This discussion is preserved as it was. New answers, comments, and votes are closed.