MoveToFinished doesn't always fire


I’m working on an enemy NPC for my game and made this script that’s supposed to follow the closest player. Once that NPC is a certain distance from the player it should attack them. If walking takes too long, it stops moving and uses a ranged attack. If it find that magnitude between player and NPC is too far, it exits the loop shown in code and restarts the full code loop so it checks if there’s another nearer player to go after.

The problem is that sometimes MoveToFinished doesn’t fire, and NPC ends up standing in front of the player until it’s there long enough for ranged attack to fire. This loops for as long as the player stands still. When I jump or move in a way that doesn’t cause this, it starts attacking me with melee attacks until I move in a way that breaks MoveToFinished again.

I looked for solutions but can’t seem to find any similar problems. Previously used MoveToFinished:Wait(), but had same problem.

Let me know if any additional information is needed.

I’m not sure if you fixed this but as a general rule MoveToFinished will fire after 8 second regardless of how far the npc actually got

My code disconnects MoveToFinished every .5 second and restarts MoveTo.

I have this so NPC constantly moves towards player. so when player moves, NPC changes direction as well rather than moving towards old direction.