Minecraft – Change Block State using Command Blocks and Carrot on Stick

minecraft-commandsminecraft-java-edition

In creative, the debug stick can change the state of a fence block so that north, south, east, and west values are all set to false. This will prevent the fence block from connecting to adjacent blocks. I would like to allow players in my survival Realm to do something similar without needing to enter creative mode.

Because the debug stick cannot be used in survival, I am trying to set up two command blocks and a carrot on a stick to achieve similar functionality. When right clicked, the carrot on a stick would locate the nearest fence block and adjust the state accordingly.

First I run this command (not in a command block):

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

Next I set up a repeating command block. This is the code I am struggling with:

/execute at @a[scores={clicked=1..},nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{display:{Name:"\"Block State Wand\""}}}}] run setblock  @e[type=fence,limit=1,sort=nearest] north false, south false, east false, west false

Then I have a second chained command block like such:

/scoreboard players reset @a[scores={clicked=1..}] clicked

I am new to command blocks and do not fully understand the correct syntax. Can someone help me figure out the code for the first command block?

Best Answer

I recently had this exact same problem while doing something similar. Funny thing, you can't actually detect an objective score. You first need to apply the objective score to the player like this:

/scoreboard objectives add clicked minecraft.used:minecraft.carrot_on_a_stick
/scoreboard player set @p clicked 0

I was confused the first time too. The only thing missing was that second command. After that, everything should work fine.