how to add names and scores in `/Tellraw` [duplicate]
I've looked through basically every post about this, and each of them is like 2-3 years old and do not work with Bedrock edition.
Here's the code I have, It displayed Test x= y= z= but does not give any actual values. The Minecraft documentation and wiki didn't have examples for these parts either.
execute @a ~ ~ ~ tellraw @a {"rawtext":[{"text": "Test", "extra":[{"selector":"@s"}]},{"text":" §ax="},{"score":{"name":"@s","objective":"Coords-X"}},{"text":" §ey="},{"score":{"name":"@s","objective":"Coords-Y"}},{"text":" §cz="},{"score":{"name":"@s","objective":"Coords-Z"}}]}I am fairly certain that the selector is messing up in both the scores and as a selector, but I am unsure on how I would get it to work.
Any ideas on how to fix this, or do it differently?
41 Answer
Update: This only works in Java edition, not Bedrock, as OP's ask is impossible in the current Bedrock edition. (Hopefully this is still useful as a guide for doing it in Java edition.)
According to the Commands/tellraw wiki page, any text you send using the /tellraw command must be a raw JSON text. On that page, the wiki says you can store scoreboard values in JSON text like this:
Scoreboard Value (requires resolution)
- score: Displays a score holder's current score in an objective. Displays nothing if the given score holder or the given objective do not exist, or if the score holder is not tracked in the objective.
- name: The name of the score holder whose score should be displayed. This can be a selector like @p or an explicit name. If the text is a selector, the selector must be guaranteed to never select more than one entity, possibly by adding limit=1. If the text is "", it shows the reader's own score (for example, /tellraw @a {"score":{"name":"","objective":"obj"}} shows every online player their own score in the "obj" objective).[4]
- objective: The internal name of the objective to display the player's score in.
- value: Optional. If present, this value is used regardless of what the score would have been.
Now let's look at your code. I'm going to break it up a bit and format it like a JSON file for ease of reading.
execute @a ~ ~ ~ tellraw @a { "rawtext":[ {"text": "Test", "extra":[{"selector":"@s"}]}, {"text":" §ax="}, {"score":{"name":"@s","objective":"Coords-X"}}, {"text":" §ey="}, {"score":{"name":"@s","objective":"Coords-Y"}}, {"text":" §cz="}, {"score":{"name":"@s","objective":"Coords-Z"}} ]
}So first, you are defining objectives. According to some Reddit posts I found, this is an example of how to do that:
{"score":{"name":"@p","objective":"TEST"}}
Which means you are doing that correctly, so that's not the issue.
Next, I asked myself if "Coords-X", "Coords-Y" and "Coords-Z" are valid objectives. So I looked it up. It turns out you have to add these as objectives before you can use them. Did you possibly run these commands to add the coordinates as objectives?
/scoreboard objectives add Coords-X dummy
/scoreboard objectives add Coords-Y dummy
/scoreboard objectives add Coords-Z dummy
Finally, it seems like you are trying to use @s to target the entity that runs the command. So you're probably trying to display the coordinates of the person who calls this command. Thus, after you define these objectives, you may need to set up command blocks, one each for X, Y and Z, and set them with this command:
/execute as @a store result score @s Coords-X run data get entity @s Pos[0] 1
Which will keep track of the coordinates so that they can be displayed.
I have no idea if this is helpful but I hope one of these things solves your problem!
5