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?
01 | local workspace = game.Workspace |
02 |
03 |
04 |
05 | function moveToPostition(moveTo) |
06 | local path = game:GetService( "PathfindingService" ):ComputeRawPathAsync(script.Parent.Parent.LowerTorso.Position, moveTo, 200 ) |
07 | local points = path:GetPointCoordinates() |
08 |
09 | for p = 1 , #points do |
10 | script.Parent:MoveTo(points [ p ] ) |
11 | repeat wait() until script.Parent.MoveToFinished |
12 | end |
13 | end |
14 |
15 | while true do |
16 | wait( 0.01 ) |
17 | walkevent = moveToPostition(workspace.BoxedHouses.Torso.Position) |
18 | end |
Alright, not sure if I'm still misunderstanding the problem somewhat, but let me know if the following works:
01 | local pathfindingCoolDown = 0.2 ; |
02 |
03 | function moveToPosition(moveTo) |
04 | local path = game:GetService( "PathfindingService" ):ComputeRawPathAsync(script.Parent.Parent.LowerTorso.Position, moveTo, 200 ) |
05 | local points = path:GetPointCoordinates() |
06 |
07 | local startTime = tick(); |
08 | for p = 1 , #points do |
09 | script.Parent:MoveTo(points [ p ] ) |
10 | script.Parent.MoveToFinished:Wait(); |
11 | if (startTime-tick() > pathfindingCoolDown) then |
12 | return |
13 | end |
14 | end |
15 | end |
16 |
17 | while wait() do |
18 | moveToPosition(workspace.BoxedHouses.Torso.Position) |
19 | end |