Hi! I'm making a map and I need help with detecting a right-click. So when a player right clicks on a Player Head (Coconut) They player will get a random effect. Is it possible? Also the player must be holding it in their hands. Any help would help. Thank you!
You need 2 systems here : Right-click detection and random generator. Both are possible in minecraft vanilla (though I dont know your version. 1.7 can not allow that, I'll talk about 1.15 commands).
There are two ways there :
This is by far the best thing you could do : Give the player a potion with a CustomModelData to apply a coconut texture on it, and effect/amplifier that you'd never use together (For the following, I'll consider it as a Nausea XVII potion).
This way, you only have to permanently test for each player having Nausea XVII effect, then trigger the mechanic : Clear nausea, give the effect and clear the resulting empty bottle.
I definitely recommend this one : it is really the first thing you should try, as it is by far safer and simpler than the second detection method, and you get left/right hand support and drink animation on top of that.
Even with no ressourcepack knowledge, I'd say it's worth to at least learn how to set a CustomModelData and draw a quick coconut model, or maybe ask community if they could provide you a ressourcepack for that.
In the case you can not make the ressourcepack thing, a popular right-click detection trick is to permanently teleport a villager right in front of the player as long as it holds the clickable item. This villager is invulnerable, silent, NoAI, has no trade, and you must have set up a score tracking each player's villager interactions.
Basically, when you have the coconut skull in hand, you keep getting the invisible villager in front of you, so if you right-click, you will in fact click the villager. As it has no trade, it won't open the interface, though the score will still track the interaction.
You simply have to track players holding a coconut who clicked on a villager.
Though this one is the most popular because it works pretty much always, it has countless drawbacks, mostly due to the villager's hitbox.
Random effect generation
This one is a lot simpler, and there is a widespread method that always work :
Basically, when you summon an entity, Minecraft gives him an UUIDLeast and an UUIDMost. These are 2 random numbers, and the fact is that they are both big and random.
Now, the trick is to summon an entity, store its UUIDMost as a score (must be divided as it can't fit in scoreboard limit), and operate a modulo on it so you can get a whole number between 0 and whatever you want. Then, you give a random effect depending of this resulting value.
The process is shown here. It's clearly explained, and I always go there when I need that system (though I haven't play Minecraft since some weeks ago)
You only have to make that and operate a modulo on the stored value (for example, making a scoreboard operation of [value]%4 will result into a random number from 0 to 3).
And then, you make a list of effects : give strenght if it matches 0, weakness for 1, leaping for 2,...