Walmart (WMT) on Tuesday agreed to pay $3.1 billion to settle its part in sweeping lawsuits multiple attorneys general filed over the country's opioid crisis.

Seventeen states, multiple cities and Native American tribes accused Walmart's pharmacies of too loosely dispensing opioid-based pain pills, many of which ended up on the streets and were resold illegally.

CVS (CVS) and Walgreens (WBA) had earlier agreed to pay a combined $10 billion to settle similar allegations.

The retailers all denied wrongdoing and said they legally filled prescriptions approved by doctors, who the pharmacies argue are to blame for overprescribing.

The nationwide litigation has fetched around $50 billion in settlements to date from various parties in the pain-pill supply chain, including drug makers, insurance companies and pharmacies.

"Pharmacies such as Walmart played an undeniable role in perpetuating opioids' destruction, and my fellow attorneys general and I are holding them accountable," Letitia James, New York's Attorney General, said in a statement. "You cannot put a price on lives lost and communities destroyed."

New York will receive about $116 million of the Walmart settlement. Native American tribes, for the first time, will receive funds from the settlements. They are entitled to about $89 million, lawyers said.

Johnson & Johnson, Cardinal, McKesson, and AmerisourceBergen in 2021 agreed to pay a combined $26 billion. Purdue Pharma offered to pay $6 billion after a judge rejected a $4 billion settlement offer.

The attorneys general say most of the settlement funds will used to prevent and treat opioid addiction and abuse. The states involved in the litigation are Pennsylvania, New York, North Carolina, Nebraska, Ohio, California, Colorado, Connecticut, Delaware, Illinois, Indiana, Iowa, Louisiana, Massachusetts, Rhode Island, Tennessee and Texas.