How can I use tellraw when a person's score goes up or down?



  • I have a scoreboard objective, EMERALDS, that automatically changes based on the number of emeralds in a player's inventory.

    When a person gains/loses a point I want to display a /tellraw to the player saying "Gained/Lost An Emerald". How can I do this?



  • You can do this using a second objective and scoreboard players operation.

    Create a second objective called DIFF

    /scoreboard objectives add DIFF dummy
    

    On a 20Hz. clock (fill clock or repeat/chain command blocks), run the following commands:

    /execute @a ~ ~ ~ scoreboard players operation @a[c=1] DIFF -= @a[c=1] EMERALDS
    /tellraw @a[score_DIFF=-1] [{"text":"You gained an emerald"}]
    /tellraw @a[score_DIFF_min=1] [{"text":"You lost an emerald"}]
    /execute @a ~ ~ ~ scoreboard players operation @a[c=1] DIFF = @a[c=1] EMERALDS
    

    To understand this, it's best to start with the last command. This command sets every player's DIFF score equal to their EMERALDS score. This means that at the beginning of each game tick, DIFF is the number of emeralds the player had in the previous game tick.

    Now, the first command subtracts the current number of emeralds from the previous one, and stores that difference in DIFF. DIFF is therefore equal to the amount of emeralds the player has less than last tick, meaning a positive/negative DIFF score means the player has lost/gained emeralds.

    In between the scoreboard commands, all we have are the two tellraw commands we want. One for gaining emeralds (DIFF ≤ −1) and one for losing emeralds (DIFF ≥ +1).




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2