Trying to make an NPC that uses pathfinding to follow a player, but whenever I test it the NPC stutters and lacks behind. Any help?
local workspace = game.Workspace
function moveToPostition(moveTo)
local path = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Parent.LowerTorso.Position, moveTo, 200)
local points = path:GetPointCoordinates()
for p = 1, #points do
script.Parent:MoveTo(points[p])
repeat wait() until script.Parent.MoveToFinished
end
end
while true do
wait(0.01)
walkevent = moveToPostition(workspace.BoxedHouses.Torso.Position)
end

11 comments
MoveToFinished is not a boolean, it's an event.
Well I don't know how to make it wait for an event so can you be a bit more descriptive?
1/2 To work off of what incapaz mentioned, you can use script.Parent.MoveToFinished:Wait() to yield properly. Not sure if that will necessarily solve your problem, though. Roblox's pathfinding yields a ton of unnecessary points (as do most pathfinding algorithms). My solution to the unnatural stuttery movement was simply raycast between each brick to gauge visibility, and then deleting extraneous
2/2 bricks. Of course, this comes with a performance cost, but I've found that it's pretty worth it for the fluid movement.
Also, as a note, you can't yield for 1/100th of a second, so your wait(0.01) is probably just better placed being written as wait(); :P Doesn't make a difference functionally, but the number is misleading.
Using script.Parent.MoveToFinished:Wait() made it stutter less, but it waits any time I turn.
I don't understand what you mean by raycasting in-between bricks. There are no bricks.
My problem is, instead of chasing me (always taking the shortest and fastest route) it more of just mirrors the moves I made half a second ago.
My mistake, 'bricks' was the wrong convention to use there, you have them denoted as points. Same concept; you can simply raycast between each of those 'points' and ensure that there is nothing in the way. That would just fix unnatural movement upon further reflection though, I'm not really sure why you're yielding at each turn. Does it wait long?
I just read your latest comment, it's probably just an issue that you aren't pathfinding often enough. See, you're just following the path until you've reached the last point, rather than actually checking for the player every so often.
Problem is, I want things to be in the way. Also its just a stutter when it stops.