Argument 1 missing or nil (really basic ai script)

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).

1 Like

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
1 Like

same error on line 14. (humanoid:MoveTo(waypoint.Position)

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

It works! but is there a way to make it smoother? it looks like this:

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.

1 Like

maybe it would look something like:

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

1 Like

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.

I think I’m obliged to tell you about SimplePath.

It covers all the hard coding stuff for you, which in turn gives so some insanely smooth AI. I just have to recommend this module, it’s that good.

1 Like