Minecraft – Why does /execute store refuse to update an NBT number when provided with a scoreboard value

minecraft-commandsminecraft-java-edition

Here are some commands for you to try in MCJE 1.16. Run the following commands in the chat:

  1. Set up a scoreboard objective. (or use an existing test objective if you hate piling them up, like me)

    /scoreboard objectives add test dummy
    
  2. Set your own score. Set it to a number you'll remember. Make it low, like 5.

    /scoreboard players set @s test 5
    
  3. Give yourself a diamond sword with enchantments. Give it a level of a different number than you picked in step 2. I pick 9.

    /give @s diamond_sword{Enchantments:[{id:"minecraft:sharpness",lvl:9s}]}
    
  4. Try to set the diamond sword's enchantment level to the number in the scoreboard. This is the command I used:

    /data modify entity @s Inventory[{id:"minecraft:diamond_sword"}].tag.Enchantments[{id:"minecraft:sharpness"}].lvl short 1 run scoreboard players get @s test
    

What this command should do is get your test score and store it in the Sharpness level of the diamond sword. But it doesn't. The level remains 9.

Here is another example with shorter commands. Do steps 1 and 2 like normal, but here's where we'll change things up:

  1. Give yourself a piece of dirt. (any item will do)

    /give @s dirt
    
  2. Modify the count of that dirt stack by setting it to the scoreboard value.

    /execute store result entity @s Inventory[{id:"minecraft:dirt"}].Count byte 1 run scoreboard players get @s test
    

Does the count of items modify? For me, it doesn't.

I tested this out using a command chain. Peeking into the command block for command #4, I can see the correct scoreboard value returned: ExpertCoder14 has 5 test, but the scoreboard value still isn't updated.

I checked all the fields, and they were all correct:

  • NBT path: I used /data get with the same NBT path and got back the old value. This means I am using the correct path to target the value to update.
  • Scale: I checked the Minecraft Wiki page on /execute store. The scale is a number that the command result will be multiplied by before storing. I set it to 1.0. This should be correct.

I also ran some additional trials:

  • I used the if subcommand to test for a number of entities instead of /scoreboard players get. Here was a command:

    /execute store result entity @s Inventory[{id:"minecraft:dirt"}].Count byte 1 if entity @e
    

    Sample output:

    Test passed, count: 14
    

But the NBT value doesn't change!

Am I doing something wrong? Is this behaviour normal? Is there a bug report that I missed? What is going on?

Best Answer

You can't modify player nbt. You have to move the item to a container modify the conatiner's item. Then loot the item back into the player's inventory. The following link shows you how to edit items in a player's hand. Here. I hope this helped.