Pathfinding Help

my path finding system is not working when using the path finding service so we replaced with a run service heartbeat that goes to the target.

now the problem is the npc walking on obstacles in this example he walks on top of heater shelf.

how to make it ignore it and walk around.

https://streamable.com/sr7fx1

local function heartbeatMove(NPCID, npc, humanoid, destination)
	local rootPart = npc.PrimaryPart
	if not rootPart then
		warn("Warning! NPC is missing a primary part! Did you forget to add one?")
		return
	end
	local targetReached = false
	local connection
	connection = RunService.Heartbeat:Connect(function(deltaTime)
		if (rootPart.Position - destination).Magnitude < 2 then -- Stop when close enough
			print("NPC:", NPCID, "Reached his target!")
			targetReached = true
			humanoid:Move(Vector3.new(0, 0, 0)) -- Stop movement
			connection:Disconnect()
		else
			humanoid:MoveTo(destination)
		end
	end)
	repeat task.wait() until targetReached -- Wait until movement is done
end

(original path finder that didn’t work well, they mostly didn’t move and failed to find path maybe because the points are close to each other)

local function robloxVersion(NPCID, npc, humanoid, destination)
	-- Compute the path
	local success, errorMessage = pcall(function()
		default_path:ComputeAsync(npc.PrimaryPart.Position, destination)
	end)
	if success and default_path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints
		waypoints = default_path:GetWaypoints()
		-- Detect if path becomes blocked
		blockedConnection = default_path.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting path blockage until path is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new path
				robloxVersion(NPCID, npc, humanoid, destination)
			end
		end)
		-- Detect when movement to next waypoint is complete
		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					-- Increase waypoint index and move to next waypoint
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
		-- Initially move to second waypoint (first waypoint is path start; skip it)
		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		print("NPC:", NPCID, "Roblox is walking to path...")
		humanoid.MoveToFinished:Wait()
		print("NPC:", NPCID, "Roblox finished walking to path...")
	else
		print("NPC:", NPCID, "Roblox Failed to find path")
		--humanoid:MoveTo(destination)
		--humanoid.MoveToFinished:Wait()
		--print("NPC:", NPCID, "Roblox finished moving by force...")
		--print("NPC:", NPCID, "Retrying path in 1s...")
		--task.wait(1)
		--robloxVersion(NPCID, npc, humanoid, destination)
	end
end

@BurgilAI

thanks for helping :pray:

1 Like

you can use regions with pathfindingmodifiers to make the path compute avoid specific araes

2 Likes