I am trying to make the NPC walk around obstacles and get to the destination. The problem is it can walk directly to the destination but it’s not going around obstacles. For some reason the for loop does not run and the waypoint array seems empty but nothing gets printed in the output even from the checks (I am not sure if I did the checks right). My friend made a script that was very similar to this one and tested it on this game and a different one with the same script. The first game didn’t work but the second game did and I have no idea why . I have tried looking at other similar devforum pages but they seem to be not exactly what I am looking for.
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local numWaypoints = 1
local goal = workspace.goal.Position
local pathService = game:GetService("PathfindingService")
function walkForward()
local path = pathService:CreatePath({AgentRadius = 4,
AgentHeight = 6, WaypointSpacing = 1, AgentCanJump = true, AgentCanClimb = true
})
path:computeAsync(person.Position, goal)
local waypoints = path:GetWaypoints()
if pathService:FindPathAsync(person.Position,goal) == Enum.PathStatus.NoPath then
error("no path")
end
if waypoints == 0 or nil then
error("waypoints is either nil or 0")
end
for i, waypoint in pairs(waypoints) do
print("for loop running")
print(waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid:MoveToFinishedWait(2)
end
--local function blockedPath(blockedWaypointIdx) not sure if i really need this
-- local path = pathService:CreatePath()
-- if blockedWaypointIdx > numWaypoints then
-- path:computeAsync(person.Position, goal)
-- if path.Status == Enum.PathStatus.Success then
-- walkForward()
-- else
-- error("path not found")
-- end
-- end
--end
--path.Blocked:Connect(blockedPath)
humanoid:MoveTo(goal)
end
while true do
walkForward()
task.wait(1)
end
It seems like it doesn’t matter which way you write it (nothing changed). I don’t think the problem is a syntax error but I don’t really know because I am just learning how to use pathfinding.
This is always gonna be true: if waypoints == 0 or nil then
You have to use if waypoints == 0 or waypoints == nil then instead, like this:
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local goal = workspace.goal.Position
local pathService = game:GetService("PathfindingService")
function walkForward()
local path = pathService:CreatePath({
AgentRadius = 4,
AgentHeight = 6,
WaypointSpacing = 1,
AgentCanJump = true,
AgentCanClimb = true
})
path:ComputeAsync(person.Position, goal)
local waypoints = path:GetWaypoints()
if pathService:FindPathAsync(person.Position, goal) == Enum.PathStatus.NoPath then
error("no path")
end
if waypoints == 0 or waypoints == nil then
error("waypoints is either nil or 0")
end
for i, waypoint in pairs(waypoints) do
print("for loop running")
print(waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinishedWait(2)
end
humanoid:MoveTo(goal)
end
while true do
walkForward()
task.wait(1)
end
I see in your code you said “humanoid:MoveToFinishedWait(2)” when it should be 'humanoid.MoveToFinished:Wait(2)"
Here is a fixed version
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local numWaypoints = 1
local goal = workspace.goal.Position
local pathService = game:GetService("PathfindingService")
function walkForward()
local path = pathService:CreatePath({AgentRadius = 4,
AgentHeight = 6, WaypointSpacing = 1, AgentCanJump = true, AgentCanClimb = true
})
path:computeAsync(person.Position, goal)
local waypoints = path:GetWaypoints()
if pathService:FindPathAsync(person.Position,goal) == Enum.PathStatus.NoPath then
error("no path")
end
if waypoints == 0 or nil then
error("waypoints is either nil or 0")
end
for i, waypoint in pairs(waypoints) do
print("for loop running")
print(waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait(2)
end
--local function blockedPath(blockedWaypointIdx) not sure if i really need this
-- local path = pathService:CreatePath()
-- if blockedWaypointIdx > numWaypoints then
-- path:computeAsync(person.Position, goal)
-- if path.Status == Enum.PathStatus.Success then
-- walkForward()
-- else
-- error("path not found")
-- end
-- end
--end
--path.Blocked:Connect(blockedPath)
humanoid:MoveTo(goal)
end
while true do
walkForward()
task.wait(1)
end
Thanks, I have a question. Randomly I keep getting this error “PathfindingService: Path request failed due to a navmesh update error”. Is this my fault?
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local numWaypoints = 1
local goal = workspace.goal.Position
local pathService = game:GetService("PathfindingService")
function walkForward()
print("1")
local path = pathService:CreatePath({AgentRadius = 4,
AgentHeight = 6, WaypointSpacing = 1, AgentCanJump = true, AgentCanClimb = true
})
print("2")
path:computeAsync(person.Position, goal)
print("3")
print("4")
local waypoints = path:GetWaypoints()
print("5")
if pathService:FindPathAsync(person.Position,goal) == Enum.PathStatus.NoPath then
error("no path")
end
print("6")
if waypoints == 0 or nil then
error("waypoints is either nil or 0")
end
print("7")
for i, waypoint in pairs(waypoints) do
print("for loop running")
print(waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait(2)
end
print("8")
--local function blockedPath(blockedWaypointIdx) not sure if i really need this
-- local path = pathService:CreatePath()
-- if blockedWaypointIdx > numWaypoints then
-- path:computeAsync(person.Position, goal)
-- if path.Status == Enum.PathStatus.Success then
-- walkForward()
-- else
-- error("path not found")
-- end
-- end
--end
--path.Blocked:Connect(blockedPath)
humanoid:MoveTo(goal)
end
while true do
walkForward()
task.wait(1)
end
Im not sure if this is what you wanted me to do but all of the numbers were printed so I think it would have to go through the if statement.
That does not make sense, as if waypoints == 0 or nil then always returns true and should output the error, can you try putting a print inside that if statement?
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local goal = workspace.goal.Position
local pathService = game:GetService("PathfindingService")
function walkForward()
print("1")
local path = pathService:CreatePath({
AgentRadius = 4,
AgentHeight = 6,
WaypointSpacing = 1,
AgentCanJump = true,
AgentCanClimb = true
})
print("2")
path:ComputeAsync(person.Position, goal)
print("3")
local pathStatus = path.Status
if pathStatus == Enum.PathStatus.NoPath then
error("no pathing found")
elseif pathStatus == Enum.PathStatus.Complete then
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
print("flag lr")
print(waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
error("path status: " .. tostring(pathStatus))
end
print("8")
humanoid:MoveTo(goal)
end
while true do
walkForward()
task.wait(1)
end