Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Unable to cast Vector3 in Pathfinding script?

Asked by 6 years ago

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.

0
your first argument is a Vector3 and your second is a userdata. add `.Position` to the end of line 2 Gey4Jesus69 2705 — 6y
0
but `:FindPathAsync()` is deprecated in favor of `:CreatePath()` so u should look into that: https://developer.roblox.com/api-reference/function/PathfindingService/CreatePath Gey4Jesus69 2705 — 6y
0
Thanks rochel89 42 — 6y

1 answer

Log in to vote
0
Answered by
Bisoph 5
6 years ago
Edited 6 years ago

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
0
I see.I'll try it later. rochel89 42 — 6y
0
When I put Position in,It says GetWayPoints is not a valid memeber of path. rochel89 42 — 6y
0
It is in line 6 btw. rochel89 42 — 6y
0
oh well that's your fault i only edited one line of code lol but i'll see if i can fix it Bisoph 5 — 6y
0
i fixedd it, check the edit Bisoph 5 — 6y
Ad

Answer this question