2

Is it possible to spawn a mob such as a skeleton in front of the player when they right click with an item such as a carrot on a stick?

3
2

Yes, this is indeed possible, and actually fairly simple. What map makers usually do for right-click detection is look at the statistics of how many times you used a carrot-on-a-stick (coas). Because right clicking with a coas increases your statistic by one, they just look if it has increased, and if it has, then they know you right clicked. First, you need to set up the statistic-checker. This can be done with a one time command:

/scoreboard objectives add rightClick minecraft.used:minecraft.carrot_on_a_stick

To break this up, you are creating a scoreboard objective named rightClick, and because of the last part, it will increase by one every time you used a coas, AKA every time you right click. The remaining commands you need to do on a repeat command block.

First, you have the summoning command. In order to summon it at the right spot, you want to execute as&at the entity that right clicked, and then summon the entity based upon those coords. First, in order to execute as an entity, you need a target selector. We are looking for any player who has a rightClick of one or higher, as we know that that means they used their coas, which means they right clicked. Because rightClick is a scoreboard objective, and not a tag, it would look like this:

@a[scores={rightClick=1..}]

Let's break that down. @a means all players. Specifying that means that execute will target all players. We don't want that. We only want players with a rightClick of one or higher. For that, we use scores={rightClick=1..}. The scores part is just telling Minecraft that you are looking at scoreboard objectives, and not tags, or NBT data. The rightClick=1.. is where the actual detection happens. The .. in Minecraft implies a range. In the case of 1.., you are saying from 1 to infinity, as I did not specify a number for the right side of that range. The rightClick= part is saying that you want to look at the specific objective rightClick, and not something else.

You now have a target selector, now you just need to build an execute around that.

First, you need to execute as all the players with that target selector:

execute as @a[scores={rightClick=1..}] run say hi

This command here will run for every player that qualifies, as each player that qualified. For example, if you have two people that right clicked, this command will run twice, with the selector @s targeting first the first person, then when it loops around again, it will run again (the second time) and @s will target the second person, instead.

Now, you just need to summon an entity in front of them. First, we need to use at. That just says that not only are we execute so that @s is equal to each player targetted, but also we are executing at the exact coordinates of that entity.

execute as @a[scores={rightClick=1..}] at @s run say hi!

Now you have a command that loops around as each player that right clicked, at their exact coordinates. Now, the last step is to summon the monster in from of them. In 1.13 Mojang made this about 40x easier, by adding local coordinates. Instead of raw numbers 12 3 4, you were then able to put ~ ~4 ~, which is 4 blocks up of where you are. But with local coordinates, you can now just put: ^ ^ ^4, which is 4 blocks in front of where the entity is facing. So, for the summon command, you just need to summon a skeleton a few blocks in front of each player, so, you would just put local coordinates instead of relative or fixed:

/summon skeleton ^ ^ ^4

To now execute upon all of this execute data (pun intended :), the final command looks like this:

/execute as @a[scores={rightClick=1..}] at @s run summon skeleton ^ ^ ^4

Oh, and one more thing, you then need to set all entity scores to zero, after this. Otherwise, you would right click, and it would constantly summon skeletons. The command to do that is: /scoreboard players set @a rightClick 0. You want to chain that command after a repeat command block with your execute command.

I know this is a lot of information, but once you've learned it, you don't need to learn it any more, so I hope this helps!

4
  • Thank you for that very thorough answer! Is there a simple way to add a timer/Cooldown to this? I understand how to create another scoreboard and add that to the execute command but I don't know how to make the time tick down once it gets set to a number over 0 Feb 9 at 20:03
  • You would add another command in the chain that constantly removes one from the scoreboard objective. Upon right click, you just have it set to a number such as 41, and activate your command when it gets to one. /execute if score @s timer matches 1..1 Will run the command only if it is true. And for the ticking down, /scoreboard players operation @s timer -= #const1 timer Because you can only do operations on scoreboard numbers with other scoreboard numbers, you need to set a random guy's #const1 score to 1, so you can subtract by one. Ask another question if confused. Feb 9 at 21:50
  • Also, note that a repeating command block repeats 20 times a second, so the number you set it to will delay for 20x seconds Feb 9 at 21:51
  • Please just ask a new question Feb 11 at 14:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.