Help making NPC's sit in random seats

Hello everyone,

I want to make NPC’s duplicate and sit in random airport seats, but I have a few problems:

  • Sometimes, NPC doesn’t sit down
  • Some NPC’s select occupied seats, therefore not enough NPC’s sitting

Here’s my script in ServerScriptService:

-- Variables

local npcModel = script:WaitForChild("NPCModel")

local set = require(script.Settings)
local max = set.maximum

local folder = workspace:WaitForChild("Passengers")

local locations = script:WaitForChild("Locations")
local airportSeats = locations:WaitForChild("AirportSeats").Value

local asTable = {}
local psTable = {}

repeat task.wait() until true

-- Insert seats inside the airport seats model into a table

function addNewSeats(model)
	if not model then return end
	
	for i, v in ipairs(model:GetDescendants()) do
		if v:IsA("Seat") then
			table.insert(asTable, v)
			
			print("Seat inserted:", v.Name)
		end
	end
end

-- Seats the NPC in a seat

function airportSit(hum)	
	if not hum then return end
	
	task.wait(1)
	
	local rand = math.random(1, #asTable)
	
	local seat = asTable[rand]
	
	local animator = hum.Animator
	local track = animator:LoadAnimation(script:WaitForChild("Sit"))
	
	if seat:IsA("Seat") and not seat.Occupant then
		task.wait(2)
		
		seat:Sit(hum)
		
		track:Play()
	elseif seat.Occupant then
		-- Find a different seat if selection is occupied
		
		task.wait(2)
		
		table.remove(asTable, rand)
		
		local replacement = math.random(1, #asTable)
		local rseat = asTable[replacement]
		
		if rseat:IsA("Seat") and not rseat.Occupant then		
			rseat:Sit(hum)
		end
	end
end

addNewSeats(airportSeats)

-- Clone the NPC's on start

for i = 1, max do
	local newModel = npcModel:Clone()
	
	newModel.Parent = folder
	
	local hum = newModel:WaitForChild("Humanoid")
	
	hum.StateChanged:Connect(function(state)
		if state == Enum.HumanoidStateType.Running then			
			coroutine.wrap(airportSit)(hum)
		end
	end)
end

Here’s a picture of the NPC not sitting:

image

I’ve also annotated the script for easier reading.

If anyone has any information or help, that would be appreciated. Thanks in advance!

I just realized, while the humanoid has the boolean Sit enabled. Seat:Sit doesn’t work.

Maybe your humanoids were already “sitting” while the script was trying to make them sit into a seat.