3

I want to have a button in the world that when pressed will roll a number between 1 and 20 using the /random roll 1..20 command and then display it to everyone in chat, something like "You rolled a 5". The problem is I don't know how to format the command in a command block.

I thought it would be simple enough but I don't know the first thing about formatting execute commands.

1 Answer 1

3

value|roll

Whether or not to notify other players of the results when extracting random numbers. If this is value, then the results are displayed only to the player using the command. If this is roll, then the results are revealed to all players.

from Minecraft Wiki

As shown in the quote, /random roll already tells all players "[player] rolled a [number] (from 1 to 20)", however, if you want to display this in a custom way (e.g. An [number] was rolled!), then you need to first create a scoreboard to store the randomly rolled value:

/scoreboard objectives add random dummy

(You can call the scoreboard any name, in this case it's "random".)

Then you can roll a random number and store it to the scoreboard using /execute:

/execute as @s store result score Random random run random value 1..20

(Use random value rather than random roll, otherwise it will tell all players before you run the last command, which will use the "[player] rolled a [number] (from 1 to 20)".)

Finally, you can run this command to send a chat message to all players that includes the scoreboard value:

/tellraw @a [{"text":"An "},{"score":{"name":"Random","objective":"random"}},{"text":" was rolled"}]

To change the text, use this base command:

/tellraw @a [{"score":{"name":"Random","objective":"random"}}]

Then add {"text":"[text contents]"} as many times as needed, replacing [text contents] with whatever text you need. Make sure to seperate each section with commas.

Source: Minecraft wiki and in-game testing.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .