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 Vector3 in Pathfinding script?

I'm trying to make the npc to move to the current place it is supposed to but it doesn't.

In the output it says,Unable to cast Vector3. the error is in line 5,any help?

local torso = script.Parent.Torso.Position
local base = game.Workspace.Base
local human = script.Parent.Humanoid

local path = game:GetService("PathfindingService"):FindPathAsync(torso,base)
local points = path:GetWayPoints()

for i,v in pairs(points) do
	local p = Instance.new("Part",workspace)
	p.Anchored = true
	p.CanCollide = false
	p.Size = Vector3.new("1,1,1")
	p.CFrame = CFrame.new(v.Position)
end

if path.Status == Enum.PathStatus.Success then
	for i,v in pairs(points) do
		human:MoveTo(v.Position)
		human.MoveToFinished:Wait()
	end
end

This script is in the model of the npc.

3 comments

0

your first argument is a Vector3 and your second is a userdata. add .Position to the end of line 2

0

but :FindPathAsync() is deprecated in favor of :CreatePath() so u should look into that: https://developer.roblox.com/api-reference/function/PathfindingService/CreatePath

0

Thanks

1 answer

In the output, it says "Unable to cast Vector3 to Instance."

You're using the base part itself and not its position.

Sometimes I make this mistake too, but thanks to the output window, I'm able to fix it quickly on my own.

Here is the code you should use...

local torso = script.Parent.Torso.Position
local base = game.Workspace.Base.Position
local human = script.Parent.Humanoid

local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(torso,base)
local points = path:GetWaypoints()

for i,v in pairs(points) do
    local p = Instance.new("Part",workspace)
    p.Anchored = true
    p.CanCollide = false
    p.Size = Vector3.new("1,1,1")
    p.CFrame = CFrame.new(v.Position)
end

if path.Status == Enum.PathStatus.Success then
    for i,v in pairs(points) do
        human:MoveTo(v.Position)
        human.MoveToFinished:Wait()
    end
end

5 comments

0

I see.I'll try it later.

0

When I put Position in,It says GetWayPoints is not a valid memeber of path.

0

It is in line 6 btw.

0

oh well that's your fault i only edited one line of code lol but i'll see if i can fix it

0

i fixedd it, check the edit

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