i’m currently testing out ai as i’ve never coded pathfinding before.
code:
local pfs = game:GetService("PathfindingService")
local torso = script.Parent.Torso
local humanoid = script.Parent.Humanoid
local path = pfs:CreatePath()
path:ComputeAsync(torso.Position, workspace.endpart.Position)
local waypoint = path:GetWaypoints()
for _, waypoints in pairs (waypoint) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait(2)
end
humanoid:MoveTo(workspace.endpart.Position)
i’m not sure why it errors. Could somebody explain?
I forgot to mention, the error is at line 9 (humanoid.MoveToFinished:Wait(2)).
removing the line results in an error at line 8. and after some testing it seems it only errors at line 8 now (even with line 9).
when using humanoid.MoveToFinished:Wait() you don’t add any extra arguments, the function waits however long it will take until the NPC is done moving, not a specified time.
your script should be more like:
for _, waypoints in pairs (waypoint) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
its because when you iterate through the waypoint table, you define the value as waypoints but when using humanoid:MoveTo(waypoint.Position) you use a totally different varible. I would suggest defining the value as something more unique.
Try:
for _, wp in pairs (waypoint) do
humanoid:MoveTo(wp.Position)
humanoid.MoveToFinished:Wait()
end
PathfindingService has never been known for being smooth lol, i suggest instead using humanoid.MoveToFinished:Wait() you constantly check if the player’s position is at least 2 studs from the way point and then move on to the next waypoint.
for _, wp in pairs (waypoint) do
humanoid:MoveTo(wp.Position)
while true do
if (torso.Position - wp.Position).Magnitude < 3 then
break
end
task.wait()
end
end
not the best performance wise, but it should work correctly
it doesn’t work LOL. doesn’t matter because i think it was just lag. but is there any way to make it update its path before it reaches the destination? i moved the part but he only moved towards it afterwards.