Path Finding Not Moving NPC

Hello,

I’m having a problem where I am running a while loop and creating a path (using PathFindingService) inside the while loop so that if a player moves it keep following them.

Problem:

I have ran into a problem where my NPC is getting stuck in place even though the path is being calculated. Sometimes with different variations of my code the NPC moves an inch every time. (I believe due to the while loop creating a path every second the bot is glitching).

Solutions I have tried:

  • Moving the pathfinding block of code outside of the while loop.
  • Using a nested while loop inside a for loop - so that it only runs once.
  • Using a debounce system to allow the bot to move all the way (not the best with debouncing).
Code

Video

(Desktop 2022.03.28 - 22.01.27.03.DVR_Trim)

I can already see an issue here.

With the “for i, v in pairs(waypoints) do” loop, you have to use Humanoid:MoveToFinished:Wait(). The problem is that, the NPC will try to move to the waypoint, but since there is no delay or wait before the loop repeats, it immediately tries to move to the other waypoint before completing the current one. It will repeat this process until it reaches the last waypoint, which makes it appear that it’s just walking straight towards the last waypoint.

If there’s still an issue, I’ll try to solve it.