E.g. there is a law saying you cant serve alcohol to minors. That in and of itself doesnt prevent beyond threat of punishment if caught.
There is also a law saying you have to check ids. Procedures like that do have a preventitive effect.
Those that do not fit the above, having no impulse control, or perhaps mental issues, then laws result in stuffing them away.
It's not perfect. Yet every human society does this, and has done this for millenia. Consider that.
Note I was responding to someone who effectly said laws don't prevent crime, and citing ways they do. Understand the context. My statement was not a 200 page dissertation on "those who might break laws" and a deep dive into societal ethics, etc.
This isn't always correct and is rather myopic.
Quite often laws will issue punishments for some behaviors, but also issue things like funding for programs that work on the roots of why particular crimes occur.
Now, laws that punish post ad hoc are always easier to measure and see, because it's much harder to prove that you prevented something that only had a probability of happening in the first place.
That still requires enforcement. The existence of a law does not imply existence of enforcement, much less wide-spread enforcement (see e.g. wage theft, auto breakins, petty theft, etc).
Any time I hear a politician say they are going to reduce a particular crime by passing a law, I wonder why existing laws aren’t enforced that likely already cover the behavior being targeted. I suppose that would mean planning, logistics, and execution - things politicians don’t know how to do.