Best way to handle pathfinding NPCs

Hii, I am making a Pathfinding controller for multiple NPCs. But I want a piece of good advice on how I would do the pathfinding side. So how it works is:

  • I have an array of a bunch of dummies.

  • I loop through the array every 1 second.

  • Find the nearest target

  • Check if the dummy can see the target through ray casting.

  • If the dummy can see the target, move to it directly. If it can’t see the target, then use pathfinding.

  • When it comes to using pathfinding, the first I do is remove that dummy from the table, so it doesn’t compute the path once again for that dummy.

  • Ok so, then I wrap the :ComputeAsync() in a pcall, then if it is a success, I move on, else, insert that dummy back in the table, then I check the path’s status, if it is a success, then move on, else insert the dummy back in. The reason I don’t remove the dummy, later on, is that since pcalls are expensive and can take longer. After all of this, I get the waypoints, and then I loop through the waypoints, and then I don’t what to do after except for moving the humanoid to the waypoints.

Any help will be heavily appreciated! I am trying to make a controller for about a month now :heart: :smiley: !

I’d recommend GnomeCodes pathfinding for his Teddy game. Details can be found here: