Why can't I path:GetWaypoints() with pathfinding?

What are you attempting to achieve?

Villager Ai with Pathfinding.

What is the issue?

Pathfinding seems to be broken for me…

whenever I call path:GetWaypoints() it returns an empty table.

What solutions have you tried so far?

  • The pathfinding api on the Roblox wiki
  • The pathfinding method used in this old 2014 Roblox tutorial video
  • Various other methods

Links

Code

-- dispeller
-- Villager AI
--https://developer.roblox.com/en-us/articles/Pathfinding

local PathfindingService = game:GetService("PathfindingService")
local NPC = script.Parent 
local humanoid = NPC.Humanoid

function walkTo(destination)
	
	-- Create the path object
	local path = PathfindingService:CreatePath()
	
	-- Compute the path
	path:ComputeAsync(NPC.HumanoidRootPart.Position, destination)
	
	-- Get the path waypoints
	local waypoints = path:GetWaypoints()
	
	-- Loop through waypoints
	warn('###'..#waypoints)
	for _, waypoint in pairs(waypoints) do
		humanoid:MoveTo(waypoint.Position)
		--humanoid.WalkToPoint = waypoint.Position
		humanoid.MoveToFinished:Wait()
		print('OK: '..tostring(waypoint.Position))
		--wait(15)
	end
end

--[[
function walkTo(pos)
	--humanoid.WalkToPoint = pos
	humanoid:MoveTo(pos)
	humanoid.MoveToFinished:Wait()
end
--]]

walkingAroundRandomly = true

local points = workspace.PrimisTown.VillagerWalkingPoints:GetChildren()

while wait() do
	if walkingAroundRandomly then
		local target = math.random(1,#points)
		local point = points[target]
		walkTo(point.Position)
	end
end

help :sob:

1 Like

Just to make sure, is the HumanoidRootPart unanchored, as well as the other parts in the humanoid?

1 Like

y e s
I guarantee you
I have been programming for 8 years

thanks for the suggestion though

Edit: Made code a little cleaner and described the problem more specifically.
I’m going to go to sleep now.
Any help is appreciated!

Be sure that all the points in VillagerWalkingPoints are reacheable and non blocked or add a function that recalculates the path if it’s blocked.

3 Likes

On second thought, WHO NEEDS SLEEP?!
I solved the issue.
It was actually a really simple bug.

image

It was all in the position of the targeted points.
Technically they’re impossible to get to because they’re too high up for the NPC to reach them.

1 Like

Was just about to say that haha, I tested your script myself and realized just that if the position can’t be reached it won’t move.

I would recommend that you add in a path blocked function in case:

  1. Something in the map somehow blocks it.
  2. It is unreachable because you made a mistake

etc.

That way its robust in design and it can ignore waypoints if needed.

Also you don’t need the constant walkingAroundRandomly if the boolean value hasn’t changed. Although you may plan to use it in future so who knows, just make sure its used at the end I guess.

Have fun. :slightly_smiling_face: