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