How did he buy a gun and pass a background check?
Nikolas Cruz Reportedly a Former Mental Health Clinic Patient Prior to Shooting
Simply being a patient is not a factor for denying a permit. A person must be adjudicated mentally defective in court, or have been committed (again, by a court or another entity with such authority) to a mental institution.
Simply checking themselves in (no adjudication), or being ordered in for observation, doesn't cut it.
The only solution is to bring on more gun laws.
/s
Bttt