How do I display arbitrary text and numbers in the scoreboard sidebar?

The sidebar is a display of "players" and their scores in one scoreboard. You can create a scoreboard like this:

/scoreboard objectives add scoreboard_id dummy "Scoreboard display text"

This creates a scoreboard with the ID "scoreboard_id" that is shown in the sidebar and in the feedback of some commands as "Scoreboard display text" and is of type "dummy", which means that nothing changes it except for commands.

To show it in the sidebar, do this:

/scoreboard objectives setdisplay sidebar scoreboard_id

But it doesn't actually show up until someone has a score in that scoreboard.

Now to the thing you actually asked about: To show arbitrary text in the sidebar, you need to set a score of a "dummy player" to a value. Example:

/scoreboard players set player_name scoreboard_id 42

This will show up like this in the sidebar:

But be careful! There is a real Minecraft account named "player_name" (archive), so if that person is on your server or plays a map that you release or anything like that, you will actually set their score to that value. There are only two ways I know around that:

  1. Use something that can't be a real player name, like "$name". But it will be displayed like that on the sidebar.
  2. Use the formatting code (archive) "$r" before the dummy player name. The problem is that this only works in functions. So you need to create a datapack with a function that does nothing except setting a scoreboard score, have it enabled and run the function instead of directly running the command.

Thanks to Lilith in the Minecraft commands Discord chat for the tip about the formatting code!