Stopping an NPC path

TL;DR: Is there a way in stopping or destroying an NPC’s path?

Whenever the player makes a loud sound near an NPC, it’s supposed to stop moving along its normal route and move towards where the sound originated from.
However, the problem is it won’t move towards the sound source until it has finished its normal route. I’m suspecting the problem lies in the while loop which only updates the NPC after every 2 seconds.
The problem: https://gyazo.com/22f0c0fb8372b1d6b5bb5131132cd256

There’s a quite a bit of code, so I’ve only posted the ones I feel relevant.

Server Script, normal movement

while wait(Patrol_Delay) do  -- NPC in normal patrol state
	for _,Guards in pairs(workspace["Lvl"..CurrentLevel].NPC.Guard:GetChildren()) do	-- Loop through all NPC in guards folder
		local guardThread = coroutine.wrap(function()
			if Guards.Humanoid.Moving.Value == false and Guards.Humanoid.Searching.Value == false then
				Guards.Humanoid.Moving.Value = true
				NPC_AI.NormalPatrol2WayPoints(Guards.Name, Patrol_Delay)	-- Call module script function
				Guards.Humanoid.Moving.Value = false
			end
		end)
		guardThread()
	end
end
1 Like

Just set the MoveTo position to the humanoidrootpart, stopping the NPC.

NPC_AI.NormalPatrol2WayPoints(Guards.Name, Patrol_Delay) -- Call module script function
The server script checks if the NPC can move and fires a function from the module script, moving the NPC.
I can’t just change the end position of the path suddenly.

Right, I decided to chuck in a break inside of the module script’s waypoint loop, that seems to have worked.

	for _, waypoint in pairs(waypoints) do	-- Loop through waypoints
		Guard.Humanoid:MoveTo(waypoint.Position)
		Guard.Humanoid.MoveToFinished:Wait()
		if Guard.Humanoid.Searching.Value == true then  -- If guard in alert mode
			break
		end
	end