Incorporate pathfinding into attacking NPC?

So I made a pathfinding script from a tutorial that makes the shortest path from the dummy to a part and it can go around walls and such. However, I want to use this for an attacking NPC that constantly changes the end goal to the nearest player and can attack them but I don’t know how, this is my code:

local pathfindingService = game:GetService(“PathfindingService”)

local StartingPoint = script.Parent.PrimaryPart
local humanoid = script.Parent.Humanoid
local EndPoint = workspace.EndPoint
local Path:Path
Path = pathfindingService:CreatePath()

local function computePath(startGoal:Vector3, endGoal:Vector3)
local RetriesLeft = 20–20 retries
local RetryInterval = 3–delay 3 seconds each time it fails

for i = RetriesLeft, 1, -1 do
Path:ComputeAsync(startGoal,endGoal)

if Path.Status == Enum.PathStatus.Success then
		return Path
	else
		warn("Path failed to compute, retrying...")
		task.wait(RetryInterval)
	end
end
error("Path failed to compute")--this will run if the loop is finished or all the retries failed

end

local function WalkHumanoid(humanoid:Humanoid, startGoal:Vector3, endGoal:Vector3)
local Path = computePath(startGoal,endGoal)

--setting up path properties
local WayPoints = Path:GetWaypoints()
local CurrectWayPointIndex = 2 --the point to go after the first point as the first waypoint is just the start position

--setup connections
local MoveToFinishConnection:RBXScriptConnection
local PathBlockedConnection:RBXScriptConnection

PathBlockedConnection = Path.Blocked:Connect(function(blockedWaypointIndex)
	warn(blockedWaypointIndex,CurrectWayPointIndex)
	if blockedWaypointIndex >= CurrectWayPointIndex then--a waypoint ahead of us is blocked! recompute the path
		--disconnect the events
		MoveToFinishConnection:Disconnect()
		PathBlockedConnection:Disconnect()
		
		humanoid.WalkToPoint = StartingPoint.Position --make our humanoid stop walking at its current position and cancel it's movement to the next waypoint
		WalkHumanoid(humanoid,StartingPoint.Position,endGoal)
		
	end
end)

MoveToFinishConnection = humanoid.MoveToFinished:Connect(function(reached)
	if reached then--if the humanoid reach the waypoint within 8 seconds
		if CurrectWayPointIndex < #WayPoints then--if we have not cycle through the last waypoint
			CurrectWayPointIndex += 1--we increase the index by 1 so that the humanoid moves to the next waypoint
			humanoid:MoveTo(WayPoints[CurrectWayPointIndex].Position)--and the same thing
			if WayPoints[CurrectWayPointIndex].Action == Enum.PathWaypointAction.Jump then
				humanoid.Jump = true
			end
		else
			print("Path reached!")
			MoveToFinishConnection:Disconnect()--disconnect the unnecessary event to avoid memory leaks
		end
	else--failed to reach waypoint within 8 seconds, the dummy probably is stuck, so let's recompute it!

		MoveToFinishConnection:Disconnect()
		WalkHumanoid(humanoid,StartingPoint.Position,endGoal)--we are changing our start goal and replacing it to the current position of our dummy instead of reusing the old one

	end
end)

--Visualize waypoints
for _, point:PathWaypoint in ipairs(WayPoints) do
	local part = Instance.new("Part")
	part.Anchored = true
	part.CanCollide = false
	part.Material = Enum.Material.Neon
	part.Color = Color3.fromRGB(255,255,255)
	part.Position = point.Position--PathWaypoint objects have a property named Position which describes their position in the form of Vector3.
	part.Parent = workspace
end
-- make humanoid walk
humanoid:MoveTo(WayPoints[CurrectWayPointIndex].Position)
if WayPoints[CurrectWayPointIndex].Action == Enum.PathWaypointAction.Jump then
	humanoid.Jump = true
end

end

WalkHumanoid(humanoid, StartingPoint.Position, EndPoint.Position)