Legislature can't prevent bad people from doing bad things. But it can prevent good people from doing bad or ignorant things, and it provides a framework to punish those who cause harm.
Every law can and does get broken, but that doesn't mean laws are useless.