So I have this code in a script inside an npc:
local pathfindingService = game:GetService("PathfindingService")
local pointModel = Instance.new("Model")
pointModel.Name = "Points"
pointModel.Parent = game.Workspace
function visualizePath(path)
-- clear old path visualization
pointModel:ClearAllChildren()
-- calculate new visualization
local points = path:GetPointCoordinates()
for _, point in ipairs(points) do
local part = Instance.new("Part")
part.Parent = pointModel
part.FormFactor = Enum.FormFactor.Custom
part.Size = Vector3.new(1,1,1)
part.Position = point
part.Anchored = true
part.CanCollide = false
end
end
function walkto(target, npc)
local start = npc.Torso.Position
local path = pathfindingService:ComputeRawPathAsync(start, target, 500)
visualizePath(path)
-- Check to see if the path was computed correctly
if path.Status == Enum.PathStatus.FailStartNotEmpty or path.Status == Enum.PathStatus.FailFinishNotEmpty then
print("Compute failed")
return {}
end
return path:GetPointCoordinates()
end
I should be able to make the npc move with this code:
position = Vector3.new(0,0,0)
pos = walkto(position, script.Parent)
for _,pos in ipairs(pos) do
script.Parent.Humanoid:MoveTo(pos)
end
However the npc doesn't seem to move at all, and if I add a print in, it prints for as many points as their are(like it should) but its prints them all almost immediately. All I need is for an npc to walk to a position, while avoiding obstacles so it doesn't get stuck. All help is appreciated :)
Note: The trail of parts using visualizePath works.
For kingdom5:
wait(3)
math.randomseed(tick())
local npcModel = script.Parent
local pfServ = game:GetService('PathfindingService')
local start = npcModel.Torso
local tycoons = {}
for _,object in ipairs(game.Workspace:GetChildren()) do
if(object.Name == "Tycoon") then
table.insert(tycoons, object)
end
end
local stop = tycoons[math.random(#tycoons)]:FindFirstChild("orderhere")
function visualizePath(path)
-- calculate new visualization
local points = path:GetPointCoordinates()
for _, point in pairs(points) do
local part = Instance.new("Part", game.Workspace)
part.FormFactor = Enum.FormFactor.Custom
part.Size = Vector3.new(1,1,1)
part.Position = point
part.Anchored = true
part.CanCollide = false
end
end
function walk(path)
for i, v in ipairs(path:GetPointCoordinates()) do
visualizePath(path)
print('moving to', v)
npcModel.Humanoid:MoveTo(v)
if not npcModel.Humanoid.MoveToFinished:wait() then
return false
end
end
return true
end
function run()
local path = pfServ:ComputeRawPathAsync(start.Position, stop.Position, 500)
if path.Status == Enum.PathStatus.Success then
while not walk(path) do
path = pfServ:ComputeRawPathAsync(npcModel.Torso.Position, stop.Position, 500)
end
else
print('path could not be found', path.Status)
end
return true
end
wait(10)
run()
I got the npc which was causing a problem as a free model. All I did to fix the problem was to use a different free model npc.

2 comments
If you found a solution to your problem then please consider writing an answer yourself.
Will do