Plants such as tobacco produce it to repel pests - nicotine interferes with the neurotransmitters of insects and kills them.