Because many plants that produce the same alkaloids are almost sterile. Microorganisms do not breed on them, or they do it in minimal quantities, they are rarely affected by fungi, insects, there is less opportunity to be eaten and not give offspring.
Some substances are created to protect against UV radiation (hello, cannabinoids), and tons of completely unobvious properties.
Narcotic substances are narcotic only when isolated in their pure form and consumed in certain quantities, but in fact, what is a real drug in a plant is also seasoned with a whole complex of amusement substances that fulfill the utilitarian properties of 'killing'. And the drugs themselves are often poison. Rare plants containing anything narcotic are safe in the wild and only have a narcotic effect when used.
So that the homosapiens can twist a joint and puff. Thank nature!
Not really an interesting question. It is much more interesting to understand why we need opioid receptors, and how and why they were formed in the process of evolution.
I read the book "Smart Plants" here just now, and so, there was a small topic dedicated to this. The biological role of narcotic substances for plants is that in fact all narcotic substances are, to one degree or another, toxins for different living beings. It's just that many of their toxins in small doses have an intoxicating effect on people. In fact, these substances are not even designed for people, but they help to scare away the main "opponents".