Pathfinding not avoiding obstacles

Is the error happening on the :MoveTo? You need to give the full error message

It should be :MoveTo after the if condition:

local nextWaypoint = waypoints[index + 1]
if not nextWaypoint then
    return  -- We have finished pathfinding
end

hum:MoveTo(nextwaypoint)
local function Navigate()
local start=waypoints[1]
local ending=waypoints[#waypoints]
local path=service:CreatePath({
AgentRadius=4,
AgentHeight=10,
AgentCanClimb=true,
AgentCanJump=true
})
path:ComputeAsync(start.Position, ending.Position)
local ways=path:GetWaypoints()
for index, waypoint in waypoints do
local nextWaypoint = waypoints[index + 1]
if not nextWaypoint then
return
end
hum:MoveTo(nextWaypoint.Position)
end
hum.MoveToFinished:Wait()
end

all works right, but it starts from player’s root part. And it doesn’t go to other waypoints. It instantly goes from root to end point when it should go to start point, to other points, before going to the end point.

local function Navigate()
    local ending = waypoints[#waypoints]
    local path = service:CreatePath({
        AgentRadius = 4,
        AgentHeight = 10,
        AgentCanClimb = true,
        AgentCanJump = true
    })
    path:ComputeAsync(root.Position, ending.Position)
    path:MoveTo(hum)
    path.StatusChanged:Wait()
end

Pathfinding should automatically avoid obstacles based on the generated path, so manually moving the character from each waypoint isn’t necessary.

Also you can add more to the costs to make sure they totally avoid some parts.

local H = math.huge
local Agents={WaypointSpacing=(5),
AgentRadius=(1),AgentHeight=(0),
AgentCanJump=(false),AgentCanClimb=(false),
Costs={Metal=H,Basalt=H,LeafyGrass=H,SmoothPlastic=H}}

i meant, the start point is

local start=waypoints[1]

, and the end point, as you guess is

local ending=waypoints[#waypoints]

.
It should go from start point, to end.
Not from root to end. Ill try changing some things in your script.

 path:ComputeAsync(root.Position, ending.Position)
    local waypoints = path:GetWaypoints()
    
    for _, wp in ipairs(waypoints) do
        hum:MoveTo(wp.Position)
        hum.MoveToFinished:Wait()
    end

Sorrry mixed that up a bit. Humaniod has the MoveTo.
I believe you’re problem may have been here… local nextWaypoint = waypoints[index + 1]
Just let it use the loop it will work as it should.

1 Like

MoveTo is not a member of Instance
The waypoints are parts
Here’s a part:

local plr=game:GetService("Players").LocalPlayer
local char=plr.Character or plr.CharacterAdded:Wait()
local hum=char.Humanoid
local root=char.HumanoidRootPart 


local service=game:GetService("PathfindingService")
local mouse=plr:GetMouse()

local waypoints={}

local function AddWaypoint()
local point=Instance.new("Part", game.Workspace)
point.Name="WP88871"
point.Position=root.Position
point.Anchored=true
point.Transparency=0.7
point.CanCollide=false
point.Size=Vector3.new(1, 0, 1, 0)
table.insert(waypoints, point)
end
local H=math.huge
local function Navigate()
local start=waypoints[1]
local ending = waypoints[#waypoints]
local path = service:CreatePath({
AgentRadius = (1),
AgentHeight = (10),
AgentCanClimb = (true),
AgentCanJump = (true),
WaypointSpacing=(5),Costs={Metal=H,Basalt=H,LeafyGrass=H,SmoothPlastic=H}
})
path:ComputeAsync(start.Position, ending.Position)
path:MoveTo(hum)
path.StatusChanged:Wait()
end

ipairs() loops through a table numerically, which is what you want when you have waypoints, you want it to loop through the waypoints from 1 to x.

There is no difference in iteration ordering between pairs, ipairs and the Generic Iteration I showed. The only functional difference is that ipairs only iterates the array part, starts from 1, and stops when it hits a nil.

All that to say, the for-loop can be shortened for 99% of usecases to the version I showed, and for Pathfinding there’s no difference at all as there are never any gaps in the Waypoints array returned by :GetWaypoints.

any fixes?

local plr=game:GetService("Players").LocalPlayer
local char=plr.Character or plr.CharacterAdded:Wait()
local hum=char.Humanoid
local root=char.HumanoidRootPart 


local service=game:GetService("PathfindingService")
local mouse=plr:GetMouse()

local waypoints={}

local function AddWaypoint()
local point=Instance.new("Part", game.Workspace)
point.Name="WP88871"
point.Position=root.Position
point.Anchored=true
point.Transparency=0.7
point.CanCollide=false
point.Size=Vector3.new(1, 0, 1, 0)
table.insert(waypoints, point)
end
local H=math.huge
local function Navigate()
local start=waypoints[1]
local ending = waypoints[#waypoints]
local path = service:CreatePath({
AgentRadius = (1),
AgentHeight = (10),
AgentCanClimb = (true),
AgentCanJump = (true),
WaypointSpacing=(5),
Costs={
Metal=H,Basalt=H,LeafyGrass=H,SmoothPlastic=H
}
})
path:ComputeAsync(start.Position, ending.Position)
path:MoveTo(hum) --errors
path.StatusChanged:Wait()
end

MoveTo is not a valid member of Instance.

The other person updated their reply to fix their mistake. But, where did path.StatusChanged come from?

Oh…
He thought i needed path.blahblah instead of hum.blahblah
I dont know what’s status and these thingd

There is no event for Paths called StatusChanged

Pathfinding doesn’t move us an inch.
No errors in console.

local plr=game:GetService("Players").LocalPlayer
local char=plr.Character or plr.CharacterAdded:Wait()
local hum=char.Humanoid
local root=char.HumanoidRootPart 


local service=game:GetService("PathfindingService")
local mouse=plr:GetMouse()

local waypointz={}

local function AddWaypoint()
local point=Instance.new("Part", game.Workspace)
point.Name="WP88871"
point.Position=root.Position
point.Anchored=true
point.Transparency=0.7
point.CanCollide=false
point.Size=Vector3.new(1, 0, 1, 0)
table.insert(waypointz, point)
end
local H=math.huge
local function Navigate()
local start=waypointz[1]
local ending = waypointz[#waypointz]
local path = service:CreatePath({
AgentRadius = (1),
AgentHeight = (10),
AgentCanClimb = (true),
AgentCanJump = (true),
WaypointSpacing=(5),
Costs={
Metal=H,
Basalt=H,
LeafyGrass=H,
SmoothPlastic=H
}
})
path:ComputeAsync(root.Position, ending.Position)
local waypoints = path:GetWaypoints()

for _, wp in ipairs(waypoints) do
hum:MoveTo(wp.Position)
hum.MoveToFinished:Wait()
end
end

I was referring to a script that also has a few modules going. Both them calls are in there.

You got them Costs set to no passage. It will not walk on any part made of them…

Costs={
Metal=H,
Basalt=H,
LeafyGrass=H,
SmoothPlastic=H
}

I was just showing you that can also be used. You can strategically place parts of that type to aid in them not walking the wrong ways in places. Like smoothly through a doorway. AgentRadius controls how far they get from things. Like how they round a corner shows it well…

New error found…
Even tho it doesn’t error in the console, it just ignores all the waypoints except start point and end point.
Here’s how it goes:

Code:

local waypointz={}

local function AddWaypoint()
local point=Instance.new("Part", game.Workspace)
point.Name="WP88871"
point.Position=root.Position
point.Anchored=true
point.Transparency=0.7
point.CanCollide=false
point.Size=Vector3.new(1, 0, 1, 0)
table.insert(waypointz, point)
end

local function Navigate()
local start=waypointz[1]
local ending = waypointz[#waypointz]
local path = service:CreatePath({
AgentRadius = (1),
AgentHeight = (10),
AgentCanClimb = (true),
AgentCanJump = (true),
WaypointSpacing=(5),
})
path:ComputeAsync(start.Position, ending.Position)
local waypoints = path:GetWaypoints()

for _, wp in ipairs(waypoints) do
hum:MoveTo(wp.Position)
hum.MoveToFinished:Wait()
end
end

I wish my current bot scripts were not so module’d out now…
Anyways there is a great resource for all this here: Character Pathfinding