How to make PathFindingService detect moving objects?


(Sorry for the video quality, I can’t upload very big files into here)

As you can see in the video above, my NPC can’t detect when something is moved in front of him. I mostly want to do this because if a player walked in front it wouldn’t look that good. Is there any way I can make it create a new path to avoid the part; while still not making it slow?

Here is my script:

local PathFindingService = game:GetService(“PathfindingService”)

local human = script.Parent:WaitForChild(“Humanoid”)

local torso = script.Parent:WaitForChild(“LowerTorso”)

local path = PathFindingService:CreatePath()

path:ComputeAsync(torso.Position, game.Workspace.EndingPart.Position)

local waypoints = path:GetWaypoints()

local runAnim = human:LoadAnimation(script:FindFirstChild(“RunAnim”))

local jumpAnim = human:LoadAnimation(script:FindFirstChild(“JumpAnim”))

runAnim.Looped = true

local function jumping()

jumpAnim:Play()

wait(.5)

jumpAnim:Stop()

end

runAnim:Play()

for i, waypoint in pairs(waypoints) do

if waypoint.Action == Enum.PathWaypointAction.Jump then

human:ChangeState(Enum.HumanoidStateType.Jumping)

spawn(jumping)

end

human:MoveTo(waypoint.Position)

human.MoveToFinished:Wait()

end

1 Like

You can use Path.Blocked and recalculate the path every time there’s an obstacle between the NPC’s path.
Alternatively, you can fire a raycast from the NPC’s HumanoidRootPart to around 5 studs in front of the NPC and if it detects a part, it’ll recalculate the path (I recommend this method since I’ve heard that Path.Blocked is not reliable).

5 Likes