For the past few days I've been working on a script that is currently really buggy and inefficient. There are a few things that I can't figure out on my own, but first and foremost, I'm wondering how I can always ensure that my NPCs will sit down when they go to a chair. For example, many of the seats are included in chair models that are contained within tables. So when attempting to reach a chair, an NPC will jump over other chairs. I believe this might be part of the problem - they jump over a chair and attempt to move to the checkpoint, but because of how ROBLOX's chairs work, they just stand on it, and because of this, they never reach the proper checkpoint. Sorry for the long post, hope someone can help!
Here's my code:
npcs = game.Workspace.ActiveNPCs:GetChildren() benchSeats = {} benches = game.Workspace.Benches:GetChildren() tableSeats = {} tables = game.Workspace.Tables:GetChildren() for _, bench in pairs(benches) do benchParts = bench:GetChildren() for _, benchPart in pairs(benchParts) do if benchPart:IsA("Seat") then table.insert(benchSeats, benchPart) end end end for _, tble in pairs(tables) do tbleParts = tble:GetChildren() for _, chair in pairs(tbleParts) do if chair.Name == "Model1" then seat = chair.Seat table.insert(tableSeats, seat) end end end print(#benchSeats) print(#tableSeats) for _, npc in pairs(npcs) do benchOrTable = math.random(1, 100) if benchOrTable >=50 then chk = math.random(1, #tableSeats) checkpoint = tableSeats[chk] checkpointPos = checkpoint.Position print(checkpoint) tableSeats[chk] = nil else chk = math.random(1, #benchSeats) checkpoint = benchSeats[chk] checkpointPos = checkpoint.Position print(checkpointPos) benchSeats[chk] = nil end path = game:GetService("PathfindingService"):ComputeRawPathAsync(npc.Torso.Position, checkpointPos, 200) points = path:GetPointCoordinates() print(path) reachedGoal = false if path.Status == Enum.PathStatus.Success then for _, point in pairs(points) do npc.Humanoid:MoveTo(point) repeat distance = (point - npc.Torso.Position).magnitude if checkpoint.Occupant ~= nil then reachedGoal = true end if npc.Humanoid.Sit == true and reachedGoal == false then npc.Humanoid.Jump = true end if point.Y >= npc.Torso.Position.Y + 1.5 and reachedGoal == false then npc.Humanoid.Jump = true end ray = Ray.new(npc.Torso.Position, npc.Torso.CFrame.lookVector*2) hit, position = game.Workspace:FindPartOnRay(ray, npc) distance1 = (position - npc.Torso.Position).magnitude if hit ~= nil then npc.Humanoid.Jump = true end wait() until distance < 3 and reachedGoal == false or reachedGoal == true wait() end wait() end wait() end print("Success!")