Texas mass shooter Seth Ator was barred from owning guns because he was mentally ill — but he was able to use a loophole in the law to get the assault-style rifle used in his massacre, according to reports.

Officials previously confirmed that Ator — who killed seven and injured 22 in Saturday’s 10-mile rampage — had failed to pass background checks, but they did not reveal why.

Law enforcement sources now say that he was a “prohibited person” because he had been diagnosed as mentally ill, according to ABC News.

The 36-year-old instead exploited a loophole in federal gun regulations by buying the AR-style weapon in a private sale, multiple law enforcement sources told ABC and the Wall Street Journal.

Private firearm sellers are not required to run background checks or even ask potential buyers if they are permitted to own weapons.

It explains why he was able to make the buy without being flagged as mentally ill, as he had been in January 2014 when trying to buy a gun, the WSJ said.

The FBI did not comment about the attempted purchase or the effectiveness of its database when asked by the WSJ.

Ator had been canned from his job as a trucker just hours before his rampage and called 911 and the FBI to complain about his bosses.

“He was on a long spiral of going down,” Odessa Police Chief Michael Gerke said, adding there were clear signs that Ator was “in trouble.”

Just 15 minutes after hanging up, Ator was pulled over by a trooper for a minor traffic infraction, setting off a brutal chain of events with him killing people indiscriminately for 10 miles.

He eventually was shot dead by cops outside a cinema in Odessa.

With Post wires