How to make an npc walk to the nearest seat?

I know how to make it walk to a seat but how do I detect it to go to the nearest one??

1 Like

You could get all the seats and check for the closest magnitude of each one

local Closest = nil

local ClosestDis = math.huge

for i,v in pairs(game.Workspace:GetDescendants()) do

 if v.ClassName ~= "Seat" then continue end

 if not Closest then Closest = v end

 if (Closest.Position - v.Position).Magnitude < ClosestDis then
 
  Closest = v
 ClosestDis = (Closest.Position - v.Position).Magnitude

 end
end

Walk To Function here and use closest


1 Like