NPC Pathfinding not working

Hello! I have a script that should set a point in a area and then make the NPC Pathfind there, but it fails for some reason. Here is my script:

Summary

local PathfindingService = game:GetService(“PathfindingService”)

local Area = Vector3.new(512.2, 0, 512)
local AreaPosition = Vector3.new(2.37, 165.827, 0.35)
local Figure = script.Parent
local Humanoid = Figure:WaitForChild(“Humanoid”)

local function getRandomPoint()
local randomPoint = Vector3.new(
math.random(-Area.X / 2, Area.X / 2),
0,
math.random(-Area.Z / 2, Area.Z / 2)
)
return randomPoint + AreaPosition
end

local function followPath(destination)
local path = PathfindingService:CreatePath({
AgentHeight = 6,
AgentRadius = 3,
AgentCanJump = true,
})

local success, errorMessage = pcall(function()
	path:ComputeAsync(Figure.PrimaryPart.Position, destination)
end)

if success and path.Status == Enum.PathStatus.Success then
	local waypoints = path:GetWaypoints()

	for i, waypoint in ipairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			Humanoid.Jump = true
		end
		Humanoid:MoveTo(waypoint.Position)
		local reached = Humanoid.MoveToFinished:Wait()
		if not reached then
			warn("NPC failed to reach waypoint:", i)
			break
		end
	end
else
	warn("Pathfinding failed:", errorMessage)
end

end

– Main loop
while task.wait(math.random(2, 7)) do
local destination = getRandomPoint()
print(“Attempting to move to:”, destination)
followPath(destination)
end

And everytime time i run it it puts out “Pathfinding failed: nil (x3) - Server - NPC_ Moving:49” As you can see i tried debugging it, Asked AI for help but nothing worked. Primary part of the NPC is the Human Root part too.

check that primary part position and destination aren’t returning nil

Hello! Sorry I’m late but have you tried checking the PathStatus? Pretty sure the reason it returns nil is because you didn’t put elseif errorMessage. Try printing the path status?

check if the AgentRadius and AgentHieght value are correct
check for path status by using Path.Status property
check if your npc can go to the random point

It returns with “num.PathStatus.NoPath”, i think i figured it out, it puts the point where it should walk to in the air, i dont know how to check if its a valid destination/ make it a valid destination.

Might be that, let us know of it fixes!