Having trouble with pathfinding

I have a pathfinding script for a zombie and I have made it so that if there is nothing inbetween the zombie and the player then the zombie will move directly at the player but if there is something then it will pathfind to get to the player. It works for the most part , but if the player constantly switches between being behind an object and having a direct path to the zombie , the AI breaks and it starts looking like the zombie is lagging . Can someone tell me why this is? By the way the script is inside of the zombies model . Here it is :
local isMoving = false
local humanoid = script.Parent.Humanoid
local model = script.Parent
local PFS = game:GetService(“PathfindingService”)
local agroDistance = 150
local blackListedParts = {game.ReplicatedStorage}
local RS = game:GetService(“RunService”)

humanoid.MoveToFinished:Connect(function()
isMoving = false
end)

game.Workspace.ChildRemoved:Connect(function(child)
if child:FindFirstChild(“Humanoid”) then
for i,v in pairs(blackListedParts) do
if v.Parent == child then
table.remove(blackListedParts,i)
end
end
end
end)

game.Workspace.ChildAdded:Connect(function(child)
if child:FindFirstChild(“Humanoid”) then
for i=1,#blackListedParts do
if blackListedParts[i].Parent == child then
break
elseif i == #blackListedParts then
for a,b in pairs(child:GetChildren()) do
table.insert(blackListedParts,b)
end
end
end
end
end)

for i,v in pairs(model:GetChildren()) do
table.insert(blackListedParts,v)
end

while true do
wait()
local entireWorkSpace = game.Workspace:GetChildren()
local possibleTargets = {}

for i,v in pairs(entireWorkSpace) do
	if v:FindFirstChild("Humanoid") and v.Parent ~= script.Parent then
		possibleTargets[i] = v:FindFirstChild("UpperTorso")
		
	end
end

local closestTarget = game.Workspace.referencePart

for i,v in pairs(possibleTargets) do
	if (closestTarget.Position - model.UpperTorso.Position).Magnitude > (v.Position - model.UpperTorso.Position).Magnitude then
		if v.Parent ~= model then
			closestTarget = v
		end
	end
end

if closestTarget then
	

	
	if (closestTarget.Position - model.HumanoidRootPart.Position).Magnitude <= agroDistance then
		
		local maginitude = (closestTarget.Position - model.HumanoidRootPart.Position).Magnitude
		local params = RaycastParams.new()
		params.FilterDescendantsInstances = blackListedParts
		params.FilterType = Enum.RaycastFilterType.Blacklist


		local rayCastResults = game.Workspace:Raycast(model.UpperTorso.Position,(closestTarget.Position-model.UpperTorso.Position).Unit*(maginitude-1),params)
		if rayCastResults  then
		--	humanoid:MoveTo(model.UpperTorso.Position)
			print(rayCastResults.Instance)
			local path = PFS:CreatePath()
			path:ComputeAsync(model.UpperTorso.Position,closestTarget.Position)
			local checkPoints = path:GetWaypoints()
			
			for i,v in pairs(checkPoints) do
				print(v.Position)
				local newMag = (closestTarget.Position - model.HumanoidRootPart.Position).Magnitude
				local newRayResults = workspace:Raycast(model.UpperTorso.Position,(closestTarget.Position-model.UpperTorso.Position).Unit*(newMag-1),params)
				if newRayResults then
					humanoid:MoveTo(v.Position)
					humanoid.MoveToFinished:Wait()

				else
					break
				end
				
			end
			
		else
			humanoid:MoveTo(closestTarget.Position)
			
		end
	else
		if isMoving == false then
			humanoid:MoveTo(model.UpperTorso.Position + Vector3.new(math.random(-30,30),model.UpperTorso.Position.Y,math.random(-30,30)))
			isMoving = true
		end
	end
end

end

First of all, please reformat your code.

Secondly, regardless of whether or not there’s an obstacle in between the paths, PathfindingService would still compute the path, so you don’t have to necessarily use raycasting to check if the path is empty.

Sorry I don’t know how to get the code to look right in dev forum , it always does that.

You reformat the code like this

CODE

I used raycasting to try and make it so that the NPC won’t continue on the same path even if the player changes position. I did not see another way around that.

There is another way to overcome that. I’m too lazy to explain but I made a tutorial: