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?

01local torso = script.Parent.Torso.Position
02local base = game.Workspace.Base
03local human = script.Parent.Humanoid
04 
05local path = game:GetService("PathfindingService"):FindPathAsync(torso,base)
06local points = path:GetWayPoints()
07 
08for i,v in pairs(points) do
09    local p = Instance.new("Part",workspace)
10    p.Anchored = true
11    p.CanCollide = false
12    p.Size = Vector3.new("1,1,1")
13    p.CFrame = CFrame.new(v.Position)
14end
15 
View all 21 lines...

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...

01local torso = script.Parent.Torso.Position
02local base = game.Workspace.Base.Position
03local human = script.Parent.Humanoid
04 
05local path = game:GetService("PathfindingService"):CreatePath()
06path:ComputeAsync(torso,base)
07local points = path:GetWaypoints()
08 
09for i,v in pairs(points) do
10    local p = Instance.new("Part",workspace)
11    p.Anchored = true
12    p.CanCollide = false
13    p.Size = Vector3.new("1,1,1")
14    p.CFrame = CFrame.new(v.Position)
15end
View all 22 lines...
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