You can't stop them from happening, even if you were to take every firearm away from all civilians people who want to commit violence and mass murder will just do so with whatever else is on hand:
en.wikipedia.org
What you can do is reduce the reasons that cause people to commit these crimes. Dismantle organized crime/drug war, heavy investment in finding a cure for common mental illnesses, and committing to reforms that reduce poverty in the country as a whole.
This is assuming of course, we didn't live in
Clown World. Where the federal government's three letter agents take advantage of the above to cause violence and push agendas:
View attachment 3457331