Entity NPC slamming into wall after chasing player

I made an entity which walks through a complex of hallways and chases the player when the player is close.

  1. What do i want to achieve?
    I want the entity to stop slamming into the walls.
    Example of the entity slamming into the wall:
    image

  2. What is the issue?
    The issue is when the entity is walking for the first time, everything is normal. The entity walks normally with no issues. But when the entity is done chasing the player, the pathfinding seems to break and the entity slams into the walls.

  3. What solutions have i tried so far?
    I have looked on the devforum for a while and i tried setting network owner to nil. None of these worked.

  4. What does the code look like?
    Here is the code i am using for the entity:

for i, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("MeshPart") then
		v:SetNetworkOwner(nil)
	end
end
local pfs = game:GetService("PathfindingService")
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
local waypoints = workspace.wps2:GetChildren()
local tpos
local target
local NotStop = true
local path
local npcpath
local sheep = true
local attacking = false

function FollowTarget(Player)
	path = nil
	wait(0.1)
	hum:MoveTo(hrp.Position)
	local Character = Player.Character
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	target = HumanoidRootPart
	--[[-local Path = pfs:CreatePath({
		AgentRadius = 3,
		AgentHeight = 6
	})
	local MovingToPosition = HumanoidRootPart.Position
	local ComputedPath = Path:ComputeAsync(hrp.Position, MovingToPosition)
	
	path = Path]]--
end

function WaitToNil()
	delay(15, function()
		if target then
			target = nil
		end
	end)
end

while sheep do
	wait(.1)
	
	if script.Parent:WaitForChild("Humanoid").Health < 1 then return end
	local randomwp = waypoints[math.random(1, #waypoints)]
	path = pfs:CreatePath({
			AgentRadius = 4,
			AgentHeight = 9
		})
	if target == nil then
		char.Humanoid.WalkSpeed = 16
		npcpath = nil
		hum:MoveTo(hrp.Position)
		path:ComputeAsync(hrp.Position, randomwp.Position)
		print("Moving To "..randomwp.Name)
	else 
		char.Humanoid.WalkSpeed = 26
		path = nil
		print("Moving to "..target.Name)
		npcpath = pfs:CreatePath({
			AgentRadius = 4,
			AgentHeight = 9
		})
		npcpath:ComputeAsync(char.HumanoidRootPart.Position, target.Position)
		if npcpath and npcpath.Status == Enum.PathStatus.Success then
			local waypoints = npcpath:GetWaypoints()
			for _, waypoint in pairs(waypoints) do  
				char.Humanoid:MoveTo(waypoint.Position)
				--[[while true do
					if target then
						local distance = (hrp.Position - waypoint.Position).Magnitude
						local distance_from_player = (char.PrimaryPart.Position - target.Position).Magnitude
						if distance < 5 then
							break
						end
						if distance_from_player < 10 then
							char.Humanoid:MoveTo((char.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
							break
						end
					end
					wait()
				end]]
			end
		end
	end

	if path and path.Status == Enum.PathStatus.Success and target == nil and NotStop == true then
		local wps = path:GetWaypoints()
		local oldtarget = target
		
		for i,v in pairs(wps) do
			if --[[target == nil]] sheep == true then
				--if target ~= oldtarget then return end
				hum:MoveTo(v.Position)
				if not target then
					hum.MoveToFinished:Wait()
				else
					break
				end
				for i, v in pairs(game.Players:GetPlayers()) do
					if v and v.Character and v.Character:FindFirstChild("Humanoid") then
						if v.Character:FindFirstChild("Crouching") and v.Character.Crouching.Value == false and v.Character.Humanoid.MoveDirection.Magnitude > 0 then
							if v.Character:FindFirstChild("HumanoidRootPart") then
								local Magnitude = (script.Parent.HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude
								if Magnitude < 30 then
									FollowTarget(v)
									WaitToNil()
									print("Attempting Follow On "..v.Name)
									hum:MoveTo(hrp.Position)
									break
									
									
								end
							end
						end
					end
				end
			elseif target ~= nil and NotStop == true and sheep == false then
				path = nil
				print("Following "..target.Name)
				hum:MoveTo(target.Position)
			end
		end
	elseif path and path.Status == Enum.PathStatus.NoPath then
		print("Failed Path")
	end
	if target ~= nil then
		if target.Parent then
			if target.Parent:FindFirstChild("Humanoid") then
				target.Parent.Humanoid.Died:Connect(function()
					target = nil
				end)
			end
		end
	end
	
	if target ~= nil and NotStop == true and sheep == false then
		print("Following "..target.Name)
		hum:MoveTo(target.Position)
	end
	
	for i,v in pairs(game.Players:GetPlayers()) do
		if target ~= nil and v ~= nil and target.Parent and v.Name == target.Parent.Name then
			local Character = v.Character
			local Humanoid = Character:FindFirstChild("Humanoid")
			local HRP = Character:FindFirstChild("HumanoidRootPart")
			
			if HRP and Humanoid and Humanoid.Health > 0 then
				local Magnitude = (script.Parent.HumanoidRootPart.Position - HRP.Position).Magnitude
				if Magnitude < 5 and Humanoid.Health > 0 and attacking == false then
					attacking = true
					char.Humanoid.WalkSpeed = 0
					local Weld = Instance.new("Weld")
					Weld.Part0 = hrp
					Weld.Part1 = HRP
					NotStop = false
					hum:MoveTo(hrp.Position)
					HRP.Anchored = true
					HRP.CFrame = hrp.CFrame:ToWorldSpace(CFrame.new(0, 0, -1))
					local Anim = hum:LoadAnimation(script.Parent.Eat)
					game.ReplicatedStorage.ChangeCam:FireClient(v, script.Parent.Camera)
					Anim:Play()
					script.Parent.Head.Scream:Play()
					wait(0.5)
					if Humanoid.Health > 0 then
						script.Parent["Right Arm"].Punch:Play()
						target = nil
						if Humanoid.Health < 0 then return end
						Humanoid.Health = 0
						local Killer = Instance.new("StringValue")
						Killer.Name = "Killer"
						Killer.Value = "Sticks"
						Killer.Parent = Humanoid
						Weld:Destroy()
					end
					-- Jumpscare code
					wait(5)
					attacking = false
				end
			end
			NotStop = true
		end
	end
	
end

I need help with this issue, so if you can, please do. Any help is appreciated.

either the corridor is illegal to move in or pathfinding is stupid use either custom pathfinding or see if the corridor is a union.