In short, patients get a script for an opioid pain killer, such as oxycodone, hydrocodone, etc.. They become addicted to it, and when they can no longer get the pills prescribed by a doctor, they then turn to heroin because it's much cheaper, can be more readily available in some cases, and it acts on the same receptors as the pills do.