Pathfinding dosent chase players

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want this monster to do a sort of chasing system where it firsts wanders around and then when it detects a person nearby it chases them
  2. What is the issue? Include screenshots / videos if possible!
    they dont really care and they either ignore whatever is happening or only chase them once before their pathfinding messes up

robloxapp-20230818-2206133.wmv (2.0 MB)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried looking into other solutions like repeating the same chase function but nothing works
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local monster = script.Parent
local humanoid = monster.Humanoid

local PathFindingService = game:GetService("PathfindingService")
monster.PrimaryPart:SetNetworkOwner(nil)


local function getPath(goal)
	local pathParams = {
		["AgentHeight"] = 10,
		["AgentRadius"] = 10,
		["AgentCanJump"] = false
	}	

	local path = PathFindingService:CreatePath(pathParams)

	path:ComputeAsync(monster.HumanoidRootPart.Position, goal.Position)

	return path
end	

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 50
	local nearestTarget

	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

			if player:WaitForChild("stats"):WaitForChild("hiding").Value == false then
				print("eeee")
				nearestTarget = target
				maxDistance = distance
			end
		end
	end

	return nearestTarget
end


local function attack(target)
	local path = getPath(target.HumanoidRootPart)

	local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude	

	if distance > 5 then
		if path.Status == Enum.PathStatus.Success then
			for index, waypoint in pairs(path:GetWaypoints()) do
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		print("die")
		--monster.Head.JumpScare:Play()

		--local playerDeath = game.ReplicatedStorage.Events:WaitForChild("playerDeath")
		--local player = game.Players:GetPlayerFromCharacter(target)
		--playerDeath:FireClient(player, monster)

		--local attackAnim = humanoid:LoadAnimation(script.Attack)
		--attackAnim:Play()
		--attackAnim.Stopped:Wait()
		--target.Humanoid.Health = 0
	end
end

local function chase(goal)
	local path = getPath(goal)
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				attack(target)
				break
			else
					humanoid:MoveTo(waypoint.Position)
					humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(goal.Position - (monster.HumanoidRootPart.CFrame.LookVector * 10))
	end
end


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

while wait(0.2) do
	patrol()
end

yeah its a pretty big thing to ask someone to edit a big script but yeah.