Making NPCs Smarter

So i made this script that basically tells an NPC to go to a certain location using PathFindingService:

Code
local service = game:GetService("PathfindingService")

wait(7)

local humanoid = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")

local path = service:CreatePath()
path:ComputeAsync(torso.Position, game.Workspace.endp.Position)
local waypoints = path:GetWaypoints()

for i, waypoint in pairs(waypoints) do
	local part = Instance.new("Part")
	part.Shape = "Ball"
	part.Material = "Neon"
	part.Size = Vector3.new(0.5,0.5,0.5)
	part.Position = waypoint.Position + Vector3.new(0,2,0)
	part.Anchored = true
	part.CanCollide = false
	part.Parent = game.Workspace
	
	if waypoint.Action == Enum.PathWaypointAction.Jump then
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
	humanoid:MoveTo(waypoint.Position)
	humanoid.MoveToFinished:Wait()
end

humanoid:MoveTo(game.Workspace.endp.Position)

Very simple script. It does in fact work like in the gif below, but i often find myself in a common situation, NPCs will just face walls and wait for them to be able to pass through (I have a script that makes some parts appear and disappear to test the NPCs “brain”)

So i was wondering how i could use the .Blocked function when NPCs find themselves in a situation where the path is blocked and they need to figure out what to do.

Any help is appreciated and thanks!

https://gyazo.com/6f7c06d39d5bb4dd2a6f72c68d70275b

Roblox explains this pretty well, if i’m not mistaken and list how to use the api in this post

1 Like

I’ll take a look at it. Thanks

So i do see how it is but not quite understand. After the path is not Enum.PathStatus.Success, being NoPath, how do i make it so the humanoids creates a new path?