We have building codes for this reason. You can't just throw up a house wherever and however you want.
We have driver's licenses for this reason. You will wear your glasses if you need them, for example, or you will get a ticket.
We have food safety inspectors and food laws (including those around food trucks that blew up) for this reason.
(source: https://itstillruns.com/history-drivers-license-5552087.html )
Speed limits are revenue sources and bear little if any relation to actual road safety. The extremely low level of enforcement of them is proof enough of that.
Driving licenses don't stop people from driving, so they are not a good analogy to what anti-gun people want to do with legal restrictions on guns. Driving licenses are analogous to permissive gun licenses in "shall issue" states, where the government can't deny you a license unless it has abundant evidence that you are simply incapable of properly handling the relevant technology (cars or guns). Considering how easy it is to pass a driving test, that's a pretty low bar. And roughly the same number of people in the US are killed each year by cars and by guns (although a much higher percentage of gun deaths are suicides, so if we just consider people killing others, cars are worse).
You set up technologies for this, yes. In this particular case, you design handguns that are much harder to have accidents with.
You do not set up legal systems to restrict everyone's liberty because some people have accidents.