Pathfinding not avoiding obstacles

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Perfect PathFinding GUI

  1. What is the issue? Include screenshots / videos if possible!

My Humanoid doesn’t avoid obstacles, or walk the right way.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes, i did search, but it didnt help.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

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)
local ways=path:GetWaypoints()
for i,v in ipairs(waypoints) do
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
end
hum.MoveToFinished:Wait()
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

It seems like you get the Waypoints as ways and not waypoints. Try replacing this line with:

waypoints = path:GetWaypoints()

Also, the for-loop line can be shortened to just:

for _, v in waypoints do

And, I assume that you haven’t copied the code correctly. Where’s all the indentation gone?

1 Like

The ‘waypoints’ is a table, containing created parts, once a button is pressed, it does

table.insert(waypoints, part)
1 Like

Right, but in your loop, the code is iterating over the waypoints table and not the actual Path. You need to change

for _, v in ways do

, with ways instead of waypoints.

Alright, ill try. I’ll check does it work, if not, ill tell you the console error.

1 Like

works, but now it doesnt start from the starting point, but starts from our position

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 function Navigate()
local ending=waypoints[#waypoints]
local path=service:CreatePath({
AgentRadius=4,
AgentHeight=10,
AgentCanClimb=true,
AgentCanJump=true
})
path:ComputeAsync(root.Position, ending.Position)
local ways=path:GetWaypoints()
for i,v in ways do
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
end
hum.MoveToFinished:Wait()
end

local function RemovePoints()
table.clear(waypoints)
for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name=="WP88871" and v:IsA("BasePart") then
v:Remove()
end
end
end

local function ClickFind()
local tool=Instance.new("Tool", plr.Backpack)
tool.RequiresHandle=false
tool.Name="Click for Waypoint"
tool.ToolTip="e"

tool.Activated:Connect(function()
local wp=Instance.new("Part", game.Workspace)
wp.Name="WP88871"
wp.Size=Vector3.new(1, 0, 1, 0)
wp.Position=mouse.Hit.p
wp.Anchored=true
wp.Transparency=0.7
wp.CanCollide=false
table.insert(waypoints, wp)
end)
end

add.MouseButton1Click:Connect(AddWaypoint)
go.MouseButton1Click:Connect(Navigate)
remove.MouseButton1Click:Connect(RemovePoints)
click.MouseButton1Click:Connect(ClickFind)
1 Like

What’s the starting point? root is where the Path starts from, which is where the player is, where do you need it to start from?

i had the idea of doing
local start=waypoints[1]
so if you could do something with it, please use it.

1 Like

Then you need to replace root.Position in the path:ComputeAsync with the starting position

should i switch the waypoints classnames from part to something else? i heard there is a thing called PathWaypoint

1 Like

Path:GetWaypoints returns a list of PathWaypoints, but why are you changing the Class? That will not fix the issue.

I’ve just tested out the thing you recommended, and i placed 3 points:
start
behind obstacle
on obstacle(end)
and it didnt go through the behind obstacle point. any fixes?

1 Like

Can you re-send the Script? Did you change the path:ComputeAsync line?

ocal 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 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 i,v in ways do
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
end
hum.MoveToFinished:Wait()
end

local function RemovePoints()
table.clear(waypoints)
for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name=="WP88871" and v:IsA("BasePart") then
v:Remove()
end
end
end

local function ClickFind()
local tool=Instance.new("Tool", plr.Backpack)
tool.RequiresHandle=false
tool.Name="Click for Waypoint"

tool.Activated:Connect(function()
local wp=Instance.new("Part", game.Workspace)
wp.Name="WP88871"
wp.Size=Vector3.new(1, 0, 1, 0)
wp.Position=mouse.Hit.p
wp.Anchored=true
wp.Transparency=0.7
wp.CanCollide=false
table.insert(waypoints, wp)
end)
end

add.MouseButton1Click:Connect(AddWaypoint)
go.MouseButton1Click:Connect(Navigate)
remove.MouseButton1Click:Connect(RemovePoints)
click.MouseButton1Click:Connect(ClickFind)
1 Like

The Navigate function only checks the first and last waypoint. You need another loop through each waypoint to visit each one:

for index, waypoint in waypoints do
    local nextWaypoint = waypoints[index + 1]
    if not nextWaypoint then
        return  -- We have finished pathfinding
    end
    
    path:ComputeAsync(waypoint.Position, nextWaypoint.Position))
    ... rest of the code ...

Also, you don’t need to do :CreatePath each time in Navigate unless you are changing the parameters. You should move it outside the function.

Also, how are you writing the script? Have you intentionally removed the indentation and spacing? It’s very difficult to read.

-- More difficult to read
local myValue=5
-- Easier to read
local myValue = 5

-- Difficult to read
local function Hello()
print("Hi!")
if myValue > 5 then
print("Hello!")
else
print("Hey!")
end
end

-- Easier to read
local function Hello()
    print("Hi!")
    
    if myValue > 5 then
        print("Hello!")
    else
        print("Hey!")
    end
end

Sorry for harder script, im not used to other scripting methods…
I’ll try your recommendation, I’ll see.

1 Like

It is no problem :laughing: Everyone needs to start somewhere, but it might be easier for people to help you if your code is more readable, just a suggestion :slight_smile:

Console:
Argument 1 missing
function Navigate()

Code:

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 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]
hum:MoveTo(nextwaypoint)
if not nextWaypoint then
return  -- We have finished pathfinding
end
end
hum.MoveToFinished:Wait()
end

local function RemovePoints()
table.clear(waypoints)
for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name=="WP88871" and v:IsA("BasePart") then
v:Remove()
end
end
end

local function ClickFind()
local tool=Instance.new("Tool", plr.Backpack)
tool.RequiresHandle=false
tool.Name="Click for Waypoint"

tool.Activated:Connect(function()
local wp=Instance.new("Part", game.Workspace)
wp.Name="WP88871"
wp.Size=Vector3.new(1, 0, 1, 0)
wp.Position=mouse.Hit.p
wp.Anchored=true
wp.Transparency=0.7
wp.CanCollide=false
table.insert(waypoints, wp)
end)
end

add.MouseButton1Click:Connect(AddWaypoint)
go.MouseButton1Click:Connect(Navigate)
remove.MouseButton1Click:Connect(RemovePoints)
click.MouseButton1Click:Connect(ClickFind)
1 Like

Did i forget about the capitals? or is there another mistake?

1 Like

I tries it with Capitals, now it says: at the return line i needed an identifier.

1 Like