Blocked pathfinding event firing repeatedly when object is small enough to jump is on path

I’m trying to make this NPC move smoothly when the path becomes blocked and the only viable path requires a jump.

I have two walls that spawn when the npc touches a pad on his way to goal which blocks him in and leaves the only way out as a 2 stud tall block the npc can jump over. Whenever the path recalculates over the jumpable wall, blocked fires every second and connects to a function that recalculates the path which makes the npc stall every second during computation. Here’s the script:

local wps = {}
local path = pfs:CreatePath()
local wpIdx = 1
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
local treasure = workspace.Treasure


local function followPath(goal)
	path:ComputeAsync(hrp.Position, goal.Position)
	wps = {}
	if path.Status == Enum.PathStatus.Success then
		wps = path:GetWaypoints()
		wpIdx = 1
		hum:MoveTo(wps[wpIdx].Position)
	else print ("No path available.")
	end

end


hum.MoveToFinished:Connect(function(reached) --MoveToFinished fires when goal hit from MoveTo
	if reached and wpIdx < #wps then
		wpIdx += 1
		if wps[wpIdx].Action == Enum.PathWaypointAction.Jump then
			hum.Jump = true
		end
		hum:MoveTo(wps[wpIdx].Position)
	end
end)

path.Blocked:Connect(function(blockedWPIdx)
	if blockedWPIdx > wpIdx then
	print("blocked")followPath(treasure)
	end
end)

wait(6)
followPath(treasure)

Does anyone have any recommended edits to make the NPC find the path in a smoother fashion? I’m new to learning NPC AI abilities. Thanks for any ideas!

You could try turning on the Navigation Mesh in Studio. This will show you where pathfinder is capable of going. If that doesn’t help you, you could also try using a predetermined point and just MoveTo that point instead of trying to calculate a path using the pathfinder.

1 Like

Thank you very much for the ideas, I didn’t even know there was a navigation mesh option.

I’m really just playing around with learning NPC AI right now, I was following a demo about dynamic pathfinding and the author was unable to figure out the problem I was having.

The visual helps but I’m still not sure why my NPC considers a jumpable spot as a block when the original author’s NPC knew how to navigate the jumps at the red spots (I assume they’re blocked points.) After my NPC dismounts the step it continues to run smoothly again.

NPC-blocked-jump
easy pictures

Red spots equal jump points. If the path is “blocked” then the path won’t be found and has to be recalculated.

1 Like