Charleston gunman Dylann Roof should never have been been able to buy the gun he used to kill nine worshippers at a church Bible study last month, the FBI director said Friday, citing a breakdown in the national background check system.

A loophole allowed the 21-year-old to buy the .45-caliber handgun with money given to him for his birthday despite a criminal record that included a recent drug possession charge, James Comey said.

"We are all sick this happened,” the FBI director told reporters. "We wish we could turn back time.”

"We are all sick this happened. We wish we could turn back time.”

- FBI Director James Comey

Roof opened fire inside the Emanuel A.M.E. Church in Charleston on June 17, killing nine in what authorities believe was an attack motivated by racial hatred. He was arrested the next day in Shelby, N.C.

The FBI's National Instant Criminal Background Check System, which does instant background checks for 30 states, should have flagged Roof on April 11, when he went to a West Columbia gun shop to guy the firearm, Comey said. But because of a series of errors, including one on Roof's rap sheet, the examiner performing the check on Roof did not know he had admitted to drug possession in a March 1 incident, which should have rendered him an "unlawful user" ineligible to purchase the gun.

More On This...

In addition to the rap sheet, there was an error β€Žon another document, which failed to reflect the geography of the area. Had the document been in order, the examination might have included contacting relevant local law enforcement agencies, including ones with records of the drug possession charge.

The background check turned up the March 1 arrest in Lexington County for drugs, but did not reveal that Roof subsequently admitted to the charge. The arrest alone would not have been sufficient to deny the sale, but an admission or conviction would have.

The background check for Roof's application was marked "delayed pending," and when there was no update for three days, the gun shop had the legal right to complete the transaction. Comey said most large chains wait for a completion of the check even if it is delayed, but said smaller retailers do not always wait, and are not required to.

It is not known why Roof opened fire inside the landmark church, which traces its roots to 1816 when African-American members of the city's Methodist Episcopal Church, led by a freed slave, broke away to form their own congregation. But he was reported to have made several racially charged threats before shooting the worshippers, who included pastor and state Sen. Clementa Pinckney, and Roof had posted several hate-filled missives on the web, including pictures of him with the Confederate flag and symbols of South African apartheid.

He is being held without bail and could face the death penalty if convicted.

Fox News Channel's Catherine Herridge contributed to this report.