Tellraw or a message in chat at a certain 𝑦 level in Minecraft Bedrock?

Tellraw or a message in chat at a certain 𝑦 level in Minecraft Bedrock? - Stylish adult female using smartphone on street

I'm making a world where the sun basically kills you and if you go above sea level during the day you'll die. The problem is that it's really easy to accidentally mine above sea level (whether it be a mountain or a hill, etc) and just die without realizing you went too far up.

I want to display a message a couple of blocks before 𝑦 63 to the player about to hit the death barrier. I'm on Bedrock.



Best Answer

You can target players within a volume by using the target selector arguments x, y, z, dx, dy, and dz.

First, to prevent the players from being spammed with tellraw messages, you need to create a scoreboard objective that keeps track of whether the player has been warned or not:

/scoreboard objectives add shownWarning dummy

Then, put the following command in a repeating, unconditional, needs redstone command block...

execute @a[scores={shownWarning=0}] ~ ~ ~ execute @s[x=~,y=63,z=~,dx=0,dy=-<number of blocks>,dz=0] ~ ~ ~ tellraw @s {"rawtext":{"text":"You are about to hit the death barrier."}}

...where <number of blocks> is the number of blocks before y=63 where you want to warn the players. This command shows the players who haven't been shown the warning and are in the warning volume the tellraw message.

Put the following in a chain, conditional, always active command block that the first command block is facing. This will set the shownWarning objective of the players who have been told the message to 1, which means they have been shown the warning.

execute @a[scores={shownWarning=0}] ~ ~ ~ execute @s[x=~,y=63,z=~,dx=0,dy=-<number of blocks>,dz=0] ~ ~ ~ scoreboard players set @s shownWarning 1

Finally, put the following in a repeating, unconditional, needs redstone command block. This will reset the shownWarning of the players who are outside of the warning zone.

execute @a[scores={shownWarning=1}] ~ ~ ~ execute @s[x=~,y=<62.90 - <number of blocks>>,z=~,dx=0,dy=-<number of blocks>,dz=0] ~ ~ ~ scoreboard players set @s shownWarning 0

For example, if the number of blocks is 5, the execute @s part of the command would be:

execute @s[x=~,y=57.90,z=~,dx=0,dy=-5,dz=0] ~ ~ ~

Link all of the repeating command blocks to a daylight sensor (using redstone) or whatever you are using to detect if it is daytime.


Explanation

Without the scoreboard objective, there would be no way to keep track if a player has already seen the warning. Otherwise, any player within the warning zone would be spammed with messages.

The reason why the commands are split up into execute @a[scores=... execute @s[x=~... and not combined like execute @a[scores=...,x=~...] is so that the ~ is set to the location of the player, not the command block.

First command

  • execute @a[scores={shownWarning=0}] ~ ~ ~ Execute as all the players who have not been shown the warning (shownWarning=0).
  • execute @s[x=~,y=63,z=~,dx=0,dy=-<number of blocks>,dz=0] ~ ~ ~ Execute if the player is also within the volume between y=63 and y=63 - <number of blocks>. As x, z, dx, and dz are also required when using y and dy, they are set to test if the player is in the x and z coordinates of the player (which will always be the case).
  • tellraw @s {"rawtext":{"text":"You are about to hit the death barrier."}} The tellraw message.

Second command

  • execute @a[scores={shownWarning=0}] ~ ~ ~ Same as the first command.
  • execute @s[x=~,y=63,z=~,dx=0,dy=-<number of blocks>,dz=0] ~ ~ ~ Same as first command.
  • scoreboard players set @s shownWarning 1 Sets the players who have not been shown the warning and are in the warning zone's shownWarning to 1. This is so they don't get spammed with messages. This command occurs after the first command (because it's a chain command block).

Third command

  • execute @a[scores={shownWarning=1}] ~ ~ ~ Execute as all the players who have been shown the warning (shownWarning=1).
  • execute @s[x=~,y=<62.99 - <number of blocks>>,z=~,dx=0,dy=-<number of blocks>,dz=0] ~ ~ ~ This is similar to the other commands except for the volume that it's testing. Instead of testing if the player is in the warning zone, it is testing if it is below it. The reason why y is 62.99 - <number of blocks> is because it is 0.01 blocks below the start of the warning zone.
  • scoreboard players set @s shownWarning 0 Sets the players who are outside of the warning zone's shownWarning to 0, which allows them to see the warning again if they enter get close to the death barrier.

Because you mentioned that you're brand new to using command blocks, here are some pages on the Minecraft wiki that you can read for more information:




Pictures about "Tellraw or a message in chat at a certain 𝑦 level in Minecraft Bedrock?"

Tellraw or a message in chat at a certain 𝑦 level in Minecraft Bedrock? - Positive young African American female student with earphones and folder using mobile phone in park
Tellraw or a message in chat at a certain 𝑦 level in Minecraft Bedrock? - Busy female talking on smartphone and checking messages during work in contemporary office
Tellraw or a message in chat at a certain 𝑦 level in Minecraft Bedrock? - Crop aged redhead female in eyeglasses using contemporary mobile phone while surfing internet in soft focus



How do you use the Tellraw command in Minecraft bedrock edition?

You can use the /say command to send a public message to all players in a Minecraft world (see also /msg, /tell or /w for private message).

How do you make messages show up in chat in Minecraft?

Minecraft Interactive Experience 28:07. Raw JSON text is the format Minecraft uses to send and display rich text to players. It can also be sent by players themselves using commands and data packs. Raw JSON text is written in JSON, a human-readable data format.



Minecraft Tellraw Command [1.19] Tutorial




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Andrea Piacquadio, Charlotte May, Karolina Grabowska, Anna Shvets