Plants produce toxins as a defense mechanism to protect themselves from being eaten by herbivores or attacked by insects. The toxins deter animals from consuming them, helping the plants survive and reproduce. Additionally, some plants use toxins to compete with other plants for resources like water, sunlight, and space.