Help with pathfinding script

So I have been making a horror game and I am currently making the monster’s AI. I have a small lack of knowledge with the pathfinding service but I found a tutorial on how to use it. The problem was the monster would always follow the player even if it was behind a wall. So I added a raycast thingy and now it kind of works. But now I am kind of stuck because the monster always casts a raycast and if the player is within the monster’s range but the player is behind the a wall, the monster gets stuck. Any help is appreciated! Here is my code (Check the walkTo function that is where im stuck):

local pathfinding = game:GetService("PathfindingService")
local plrs = game:GetService("Players")
local runservice = game:GetService("RunService")

local guy = script.Parent
local hum = guy:WaitForChild("Humanoid")
guy.PrimaryPart:SetNetworkOwner(nil)

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local WalkSpeed = guy:GetAttribute("WalkSpeed")
local SprintSpeed = guy:GetAttribute("SprintSpeed")
local Damage = guy:GetAttribute("Damage")
local MaxDistance = guy:GetAttribute("MaxDistance")

local a = script.Parent:WaitForChild("RaycastStart")

local exludeList = {}

local spawnPoint = guy:WaitForChild("HumanoidRootPart").Position

for i, v in pairs(workspace:GetDescendants()) do
	if v:IsA("BasePart") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
		if v.Transparency > 0 and v.Transparency < 1 then
			table.insert(exludeList, v)
		end
	end
end

local function getPath(destination)
	local path = pathfinding:CreatePath({
		AgentHeight = 6;
		AgentRadious = 3;
		AgentCanJump = false;

		Costs = {
			Water = 100;
			DangerZone = math.huge
		}
	})
	
	path:ComputeAsync(guy.HumanoidRootPart.Position, destination.Position)
	
	return path
end

local function findTarget()
	local nearestTarget
	local maxDistance = MaxDistance
	
	for index, player in pairs(plrs:GetPlayers()) do
		if player.Character then
			local target = player.Character
			local distance = (guy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

			if distance < MaxDistance then
				nearestTarget = target
			end
		end
	end
	
	return nearestTarget
end

local function Capture(target)
	local distance = (guy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	if distance > 3 then
		hum:MoveTo(target.HumanoidRootPart.Position)
	else
		print("caught you >:)")
	end
end

local function resetAndGoToSpawn()
	guy:MoveTo(spawnPoint)
end

local function raycastToPlayer(target)
	local target = target:WaitForChild("Target")

	local dir = target.Position - a.Position

	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = exludeList

	local raycastResult = workspace:Raycast(a.Position, dir, params)
	
	if raycastResult and raycastResult.Instance then
		if raycastResult.Instance == target then
			return true
		else
			return false
		end
	end
end

local function walkTo(destination)
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target then
				local shouldCapture = raycastToPlayer(target)
				if shouldCapture == true then
					Capture(target)
					guy.Humanoid.WalkSpeed = SprintSpeed
					break
				end
			else
				guy.Humanoid.WalkSpeed = WalkSpeed
				hum:MoveTo(waypoint.Position)
				hum.MoveToFinished:Wait()
			end
		end
	else
		hum:MoveTo(destination.Position - (guy.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

local function patrol()
	local waypoints = workspace.Waypoints:GetChildren()
	local randomWaypoint = math.random(1, #waypoints)
	walkTo(waypoints[randomWaypoint])
end

while task.wait(0.01) do
	patrol()
	if guy.HumanoidRootPart.Position.Y < -200 then
		resetAndGoToSpawn()
	end
end

I am a little confused, is your issue regarding when the monster is close to a player but a player goes behind a wall and it stops? If so isn’t that what your code is doing with your raycasting? Please clarify your problem with me so I can try to fix it. By the way, in the past I thought roblox’s default pathfinding AI was pretty bad so instead I tried using modules. I liked using SimplePath - Pathfinding Module and EZ Pathfinding V5 with the latter being more preferable for me. Sorry, this was the best advice I could offer you, Good Luck!

Thanks for replying and suggesting me the pathfinding modules. I actually have 2 problems. The first one is if he does find a player within his range but he does not find anyone with the raycast, he gets stuck and does not move at all. My other problem is, if he does find a player within his range and with the raycast, he moves towards the player but if the player suddenly goes behind a wall, he goes back to a walking to waypoints state. What I want is, instead of going back to waypoints, he goes to the last position a player was seen and if he does find someone during that time, he chases that player