How do i repeat a function without my character pausing for a second

there is this thing that chases players but theres a script so it keeps updating path by breaking but when that happens he has to pause for a while creating choppy movement


 local function attack(target)
	local path = getPath(target.HumanoidRootPart)
	local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude	

	if distance > 3 then
		local waypoints = path:GetWaypoints()
		for index, waypoint in pairs(waypoints) do
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
			if (waypoints[#waypoints].Position - target.HumanoidRootPart.Position).Magnitude > 5 then
				-- recalculate path
				print ("Player moved, Recalculating path")
				break
			end
		end
	else

		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

I cant make the function recursive because the player has to hide and when that happens the entity has to return to its normal wandering phase