Pathfinding ":MoveTo()" doesnt work

I am trying to get the AI bot to follow the player. I have made a function which gets all of the players near the AI bot, and it chooses the closest person, that works, but

This script doesnt work for some reason (There is nothing in the output). The AI bot doesn’t move:

function pathToLocation(target)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(Btorso.Position, target.Position)
	local waypoints = path:GetWaypoints()

	for _,waypoint in ipairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			Bhumanoid.Jump = true
		end
		Bhumanoid:MoveTo(waypoint.Position)
	end

end

"Btorso" is the AI torso
"Bhumanoid is the AI humanoid"
"target" is the player's torso

Can you do this

local success, errorMessage = pcall(function()
	path:ComputeAsync(Btorso.Position, target.Position)
end)
print( success, errorMessage, path)

and say what it prints? Sometime pathfinds can fail, so make sure to design the code to handle events where no path is found etc.

Not only what Skidouski mentioned, but also check the PathStatus, making sure it is Success. Otherwise, the path was impossible to calculate.

1 Like

It prints:
true nil Instance

I see, as creeperCompany5 said you could try check the path.Status

local success, errorMessage = pcall(function()
	path:ComputeAsync(Btorso.Position, target.Position)
end)
print(success, errorMessage, path.Status)

Depending on what it prints you can see what went wrong.

Hey sorry for the late reply. it was a silly mistake, i fixed it

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.