Monster wont wonder randomly after detecting a target

Hello! Im currently attempting to create a “smart” Monster that will lock onto a target until it exists its vision radius. Once the target is out the monster will continue to wonder randomly untill it finds a new target.

For some reason whenever i move the target out of radius, the monster will continue to follow the target. Even when i destroy the target it keeps pathfinding its way towards the targets last location.

Here is the code i used


function RandomWalk()
	local xRan = math.random(-100, 100)
	local zRand = math.random(-100,100)
	local goal = model.PrimaryPart.Position + Vector3.new(xRan,0,zRand)
	
	local path = PFS:CreatePath()
	path:ComputeAsync(model.PrimaryPart.Position, goal)
	local points = path:GetWaypoints()
	for i, v in pairs(hum:GetPlayingAnimationTracks()) do
		if v.Name ~= "Void Walk" then
			v:Stop()
		end
	end
	local anim = hum.Animator:LoadAnimation(walk)
	anim:Play()
	
	if path.Status == Enum.PathStatus.Success then
		for i, point in pairs(points) do
			if point.Action == Enum.PathWaypointAction.Jump then
				hum.Jump = true
			end
			hum:MoveTo(point.Position)
			local timeout = hum.MoveToFinished:Wait()
			if not timeout then
				print("stuck!!")
				hum.Jump = true
				RandomWalk()
			end
		end
	else
		print("Path failed :(")
		task.wait(2)
		RandomWalk()
	end
end

function findPath(target)
	local path = PFS:CreatePath()
	path:ComputeAsync(model.PrimaryPart.Position, target.Position)
	local points = path:GetWaypoints()
	for i, v in pairs(hum:GetPlayingAnimationTracks()) do
		if v.Name ~= "Void Run" then
			v:Stop()
		end
	end
	local anim = hum.Animator:LoadAnimation(run)
	anim:Play()

	for i, point in pairs(points) do
		if path.Status == Enum.PathStatus.Success then
			for i, point in pairs(points) do
				if point.Action == Enum.PathWaypointAction.Jump then
					hum.Jump = true
				end
				hum:MoveTo(point.Position)
				local timeout = hum.MoveToFinished:Wait()
				if not timeout then
					print("stuck!!")
					hum.Jump = true
--					findPath(target)
					break
				end

				if checkSight(target) then
					repeat
						task.wait(1)
						if target == nil then 
							break
						elseif	target.Parent == nil then 
							break 
						end
						hum:MoveTo(target.Position)
					until checkSight(target) == false or target == nil
					break
				end
				
				if (model.PrimaryPart.Position - points[1].Position).Magnitude > 20 then
					findPath(target)
					break
				end
			end	
		end
	end
end


function checkSight(target:BasePart)	
	local parms = RaycastParams.new()
	parms.FilterType = Enum.RaycastFilterType.Exclude
	parms.FilterDescendantsInstances = {model}
	local ray = workspace:Raycast(model.PrimaryPart.Position, (target.Position - model.PrimaryPart.Position).Unit * 50, parms)
	if ray then
		if ray.Instance:IsDescendantOf(target.Parent) and math.abs(ray.Position.Y - model.PrimaryPart.Position.Y) < 3 then
			print("target located")
			return true
		end
	else
		return false
	end
end


function findTarget()
	local distance = 150
	local target = nil
	
	local targets = {}
	local potentialTargets = {}
	
	print(target)
	for i, v in pairs(workspace:GetChildren()) do
		local hum = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("HumanoidRootPart")
		if hum and torso and v.Name ~= model.Name then
			if (model.PrimaryPart.Position - torso.Position).Magnitude < distance then
				table.insert(potentialTargets, torso)
			end
		end
	end 
	
	if #potentialTargets > 0 then
		for i, v in pairs(potentialTargets) do
			if checkSight(v) then
				table.insert(targets, v)
				print("yay0")
			elseif #targets == 0 and (model.PrimaryPart.Position - v.Position).Magnitude < distance then
				print((model.PrimaryPart.Position - v.Position).Magnitude)
				target = v
				distance = (model.PrimaryPart.Position - v.Position).Magnitude
			end
		end
	end
	
	if #targets > 0 then
		distance = 150
		print("yay2")
		for i, v in pairs(targets) do
			if (model.PrimaryPart.Position - v.Position).Magnitude < distance then
				target = v
				distance = (model.PrimaryPart.Position - v.Position).Magnitude
			end
		end
	end
	
	if target then
		---------------------------
		--Void Scream and Effects
		print("EEEEEEEEE")
	end
	print(target)
	return target
end


function main()
	local target = findTarget()
	print(target)
	if target then
		hum.WalkSpeed = 20
		hum.JumpPower = 10
		findPath(target)
	else
		print("lalalalla!")
		hum.WalkSpeed = 4
		hum.JumpPower = 2
		RandomWalk()
	end
end

while task.wait(1) do
	if model.Dependents.Health.Value <= 0 then
		break
	end
	main()
end

Heres where i think the problem is located


function findPath(target)
	local path = PFS:CreatePath()
	path:ComputeAsync(model.PrimaryPart.Position, target.Position)
	local points = path:GetWaypoints()
	for i, v in pairs(hum:GetPlayingAnimationTracks()) do
		if v.Name ~= "Void Run" then
			v:Stop()
		end
	end
	local anim = hum.Animator:LoadAnimation(run)
	anim:Play()

	for i, point in pairs(points) do
		if path.Status == Enum.PathStatus.Success then
			for i, point in pairs(points) do
				if point.Action == Enum.PathWaypointAction.Jump then
					hum.Jump = true
				end
				hum:MoveTo(point.Position)
				local timeout = hum.MoveToFinished:Wait()
				if not timeout then
					print("stuck!!")
					hum.Jump = true
--					findPath(target)
					break
				end

				if checkSight(target) then
					repeat
						task.wait(1)
						if target == nil then 
							break
						elseif	target.Parent == nil then 
							break 
						end
						hum:MoveTo(target.Position)
					until checkSight(target) == false or target == nil
					break
				end
				
				if (model.PrimaryPart.Position - points[1].Position).Magnitude > 20 then
					findPath(target)
					break
				end
			end	
		end
	end
end

Heres an example of the issue

There are zero errors in the output and ive edited the script in various ways. Unfortunately i couldnt find the issue.

It would be a big help if someone were to point out where the issue lies. Thank you for reading :slightly_smiling_face: