Why are drugs like cocaine illegal? If caffeine and alcohol are used for there psychoactive effects. Why isn't cocaine in moderation legal too?

Read your history. Since the beginning of civilization, every human society has had its own particular customs, traditions & laws re what's "licit" & what's ILlicit. Not just psychoactive chemicals, but everything, e.g. marriage. The roots of what substances are legal & which aren't are not rational: they're social, cultural & historical. There are many books about this fascinating subject for the general reader.