Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why I use PathfindingService but it looks like a simply MoveTo()?

Asked by 1 year ago

So, the script works, but I'm using PathfindingService for a NPC but for some reason it's like i do a MoveTo(), like, no brain, I think that I'm too distracted to figure it out, so pls help

I leave it to a Mediafire link bc the script it's too long for upload here www.mediafire.com/file/54ozsoccd52239e/Nuevo+documento+de+texto.txt/file

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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:

  1. 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.

  1. Make sure all necessary parts (NPC, target, etc.) are correctly set up in your scene and have proper collision settings.

  2. Ensure that the Raycast is not causing unexpected behavior by either blocking the path or not detecting a player/entity properly.

  3. 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

Ad

Answer this question