Help with Bus System

Hello,
I wrote a bus system script to handle my bus system, however there is a problem:
Only one player can be in the bus at a time! When a player is in the bus, no one else can get in until they leave, then only 1 person can be in the bus. In other words, only 1 person can be in the bus at a time.

Script:

local replicatedStorage = game:GetService('ReplicatedStorage')
local teleportService = game:GetService('TeleportService')
local tweenService = game:GetService('TweenService')

local id = 6215747378
local events = replicatedStorage.Remotes
local players = game:GetService('Players')
local busUI = script.BusUI
local enter = workspace.Enter1

local bus = workspace.Bus1
local body = bus.Body
local seating = body.Seating

local busPlayers = {}
local busTime = 30
local busMaxPlayers = 12
local busMinPlayers = 1
local joinable = true
local seconds = 30

local function AddPlayer(part)
	if not part.Parent:FindFirstChildOfClass('Humanoid') then
		return
	end
	
	
	local character = part.Parent
	local player = players:GetPlayerFromCharacter(character)
	if not player then return end
	if not joinable then return end
	
	if table.find(busPlayers,player) then return end
	if #busPlayers >= busMaxPlayers then return end
	
	for _, availableSeat in ipairs(seating:GetChildren()) do
		if availableSeat.Occupant then return end
		character.Humanoid.JumpPower = 0
		character.Humanoid.WalkSpeed = 0
		table.insert(busPlayers,player)
		enter.TeleportPad.BillboardGui.Players.Text = '('.. tostring(#busPlayers)..'/'.. tostring(busMaxPlayers)..' players)'
		availableSeat:Sit(character.Humanoid)
		local ui = busUI:Clone()
		ui.BusNumber.Value = 1
		ui.Info.Text = 'The bus will leave in '.. tostring(seconds) .. ' seconds'
		ui.Parent = player.PlayerGui
		break
	end
	
end

local function RemovePlayer(player)
	if not player then return end
	for key, value in ipairs(busPlayers) do
		if value == player then
			table.remove(busPlayers,key)
			break
		end
	end
	if not player.Character then return end
	if not player.Character:FindFirstChildOfClass('Humanoid') then return end
	player:LoadCharacter()
	enter.TeleportPad.BillboardGui.Players.Text = '('.. tostring(#busPlayers)..'/'.. tostring(busMaxPlayers)..' players)'
end

coroutine.wrap(function()
	while true do
		for i = busTime,0,-1 do
			seconds = i
			enter.TeleportPad.BillboardGui.Time.Text = 'Bus #1 will leave in '.. tostring(i) ..' seconds'
			for _, player in ipairs(busPlayers) do
				events.Bus1Event:FireAllClients(i)
			end
			wait(1)
		end
		if #busPlayers >= busMinPlayers then
			local accessCode = teleportService:ReserveServer(id)
			joinable = false
			for _, waitingPlayer in ipairs(busPlayers) do
				game.ServerStorage.Assets.TeleportingGui:Clone().Parent = waitingPlayer.PlayerGui
			end
			tweenService:Create(enter.TeleportPad.BillboardGui.Time,TweenInfo.new(.3),{TextColor3 = Color3.fromRGB(255,25,0)}):Play()
			enter.TeleportPad.BillboardGui.Time.TextColor3 = Color3.fromRGB(255,0,0)
			enter.TeleportPad.BillboardGui.Time.Text = 'TELEPORTING'
			teleportService:TeleportToPrivateServer(id,accessCode,busPlayers)
			repeat wait() until #busPlayers == 0
			enter.TeleportPad.BillboardGui.Time.TextColor3 = Color3.fromRGB(255,255,255)
			joinable = true
			enter.TeleportPad.BillboardGui.Players.Text = '('.. tostring(#busPlayers)..'/'.. tostring(busMaxPlayers)..' players)'
		end
	end
end)()

enter.TeleportPad.Touched:Connect(AddPlayer)
replicatedStorage.Remotes.ExitEvent.OnServerEvent:Connect(RemovePlayer)
players.PlayerRemoving:Connect(RemovePlayer)

Thank you! There are not errors in the output.

I think the problem is that you are using return in the loop, which causes the function to terminate

Try to change it for this

  for _, availableSeat in ipairs(seating:GetChildren()) do
    if availableSeat.Occupant == nil then
        character.Humanoid.JumpPower = 0
        character.Humanoid.WalkSpeed = 0
        table.insert(busPlayers,player)
        enter.TeleportPad.BillboardGui.Players.Text = '('.. tostring(#busPlayers)..'/'.. tostring(busMaxPlayers)..' players)'
        availableSeat:Sit(character.Humanoid)
        local ui = busUI:Clone()
        ui.BusNumber.Value = 1
        ui.Info.Text = 'The bus will leave in '.. tostring(seconds) .. ' seconds'
        ui.Parent = player.PlayerGui
        break
    end
end
1 Like