Unable to cast instance to vector3 error on pathfinding AI

Hey everyone I hope you are having a good day. Today I was working on my very basic pathfinding AI to try to fix an error. I finally fixed it but I got a new error “Unable to cast instance to vector3.” I had to tune my script to make sure it would not run until the torso of the humanoid was loaded in. I tried changing my variable “local destination = script.Parent:WaitForChild(“Torso”) or script.Parent.PrimaryPart” to
local destination = script.Parent:WaitForChild(“HumanoidRootPart”) or script.Parent.PrimaryPart
but I get an infinite yield error so I switched it back and I still get the vector3 error. The error happens on line 12 where the NPC computes a path.
My Script


local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("Torso") or script.Parent
local destination = script.Parent:WaitForChild("Torso") or script.Parent.PrimaryPart


--path maker
local path = pathfindingService:CreatePath()

--copute path 
path:ComputeAsync(body.Position, destination)


-- get waypoints
local waypoints = path:GetWaypoints()

for k, waypoint in pairs(waypoints) do
	humanoid:MoveTo(waypoints.Position)
	
	if waypoints.Action == Enum.PathWaypointAction.Jump then
	
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
	
	humanoid.MoveToFinished:Wait()
end	

![LocationNPCtorso|338x293](upload://jjFdIaa8eb9VfaOmzI40lpcd3t7.png)
![Humanoiderror|338x64](upload://pkFjAbEq4GyY7kB4KgIrN2KuUrp.png)
2 Likes

Nevermind. My friend figured out that the waypoints should be waypoint and for some reason it was not getting the pathfinding service.

2 Likes

new script

local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("Torso")
local destination = game.Workspace.Destination.Position

local PathfindingService = game:GetService("PathfindingService")

--path maker
local path = PathfindingService:CreatePath()

--copute path
path:ComputeAsync(body.Position, destination)


-- get waypoints
local waypoints = path:GetWaypoints()

for k, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)

if waypoints.Action == Enum.PathWaypointAction.Jump then

humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
4 Likes