Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Unable to cast instance to vector3 roblox pathfinding. How do i solve this?

I am trying to make an NPC walk to a part via pathfinding. when I try to run the script the output states the following: 'Unable to cast Instance to Vector3'

This is my code:

local PathfindingService = game:GetService("PathfindingService")

local human = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")

local path = PathfindingService:CreatePath()
path:ComputeAsync(torso, game.Workspace.endpart.Position)
local waypoints = path:GetWaypoints()


for i, waypoint in pairs(waypoints) do
	if waypoint.Action == Enum.PathWaypointAction.Jump then
		human:ChangeState(Enum.HumanoidStateType.Jumping)
	end
	human:MoveTo(waypoint.Posistion)
	human.MoveToFinished:Wait(2)
end


human.MoveTo(game.Workspace.endpart.Position)

Does anybody know why this is not working and how to solve this? Thanks in advance.

2 comments

0

On line 7, the first argument is the torso, an Instance, while it takes a Vector3.

0

eenheurn, your code looks way too similar to TheDevKing's tutorial on PathFinding. Please make sure you follow exactly what the person is telling you to do.

1 answer

Like @deeskaalstickman649 said, Torso is an instance, whilst :ComputeAsync requires a vector3 argument of start to finish!

##Solution:

To fix this, all you have to do is where it says torso in line 7 add .Position

local PathfindingService = game:GetService("PathfindingService")

local human = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")

local path = PathfindingService:CreatePath()
path:ComputeAsync(torso.Position, game.Workspace.endpart.Position)
local waypoints = path:GetWaypoints()


for i, waypoint in pairs(waypoints) do
    if waypoint.Action == Enum.PathWaypointAction.Jump then
        human:ChangeState(Enum.HumanoidStateType.Jumping)
    end
    human:MoveTo(waypoint.Posistion)
    human.MoveToFinished:Wait(2)
end


human.MoveTo(game.Workspace.endpart.Position)

Reference(s):

Path:ComputeAsync

4 comments

2

Is it best to use HumanoidRootPart because R6 rigs and R15 both have HumanoidRootParts or am I wrong

0

yes, you're correct

0

i would edit it but i want it to make it simple for him it understand, although it's better to use HumanoidRootPart

1

imagine if there is another evolution, probably like r69 and they don't have a humanoid root part LOl

This discussion is preserved as it was. New answers, comments, and votes are closed.