let's try to understand its structure and see if we can spot any issues with your PathfindingService implementation.
First, you seem to be using a custom function FindTorso to determine the target position. The function checks for a Torso (or other body part) within a specified distance from the given position.
In the main loop, you're creating a pathfinding object and setting up destination and torso variables. You then check if the destination and torso exist, and if the destination's humanoid has health greater than 0.
The script computes the path and checks if the NPC has detected a player or other entity in their vision range (using Raycast), and sets the NPC to either chase them or move within their moving area.
The actual path following seems to happen in this section:
```lua
if (Waypoints and #Waypoints > 0) then
...
for i, waypoint: PathWaypoint in Waypoints do
...
Humanoid:MoveTo(waypoint.Position - Vector3.new(0, 15.5))
end
...
end
```
There might be potential issues here:
- In this section, you subtract 15.5 units from the Y-axis of the position of each waypoint. This could be causing your NPC to move as if it's using a simple
MoveTo() function, especially if the waypoints are being positioned closer to the ground than expected:
```lua
Humanoid:MoveTo(waypoint.Position - Vector3.new(0, 15.5))
```
You could try removing or adjusting this value to see if it improves pathfinding behavior.
Make sure all necessary parts (NPC, target, etc.) are correctly set up in your scene and have proper collision settings.
Ensure that the Raycast is not causing unexpected behavior by either blocking the path or not detecting a player/entity properly.
Debugging: Place prints or debug visuals at waypoints to ensure they are being calculated correctly.
If the issue persists, try simplifying the script to pinpoint what might be causing the unwanted behavior. You can start with a basic example, ```lua
local PathfindingService = game:GetService("PathfindingService")
local humanoid_NPC = --[[your npc humanoid here]]--
local target = --[[your target part here]]--
local function followPath(path, humanoid)
local waypoints = path:GetWaypoints()
for i, waypoint in ipairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid.MoveToFinished:Wait()
end
end
local path = PathfindingService:CreatePath()
path:ComputeAsync(humanoid_NPC.Torso.Position, target.Position)
if path.Status == Enum.PathStatus.Success then
followPath(path, humanoid_NPC)
else
print("Failed to compute path")
end