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.