Make a title appear on a player screen on touching a block

minecraft-commandsminecraft-java-edition

I am building a map in Minecraft (Java Edition). How do we make it so that if a player touches or lands on a specific block a title will appear on the screen? For example /title @p title {"text":"Level 2","color":"red"}. I want this to happen upon touching a block. But I want it to run once and never again, unless the player touches a red carpet or quarts. This is what I want: a repeating system, basically a touching block system on touching a block, a title will appear and never again, but on touching another block the system will reset and we can touch that same block again. Please tell me how to make this possible.

Best Answer

Since it wasn't clear what "Touching" a block means, I choose "Land on" as the solution

First, let us create a scoreboard to track which was the last stepped on block

/scoreboard objectives add last_stepped dummy

Here is the first command, let us call it [If statement], just to reference it later in this answer.

/execute as @a unless score @s last_stepped matches <N> at @s if block ~ ~-1 ~ minecraft:<YourBlock> run <Result>

Where [@a] is the players you want to target (You can learn more about target selector here), [last_stepped] is the name of the scoreboard we created before, [N] is the ID we gave the block (We will choose an ID for each block later, this isn't any ingame actual ID), [YourBlock] is the block he has to stepon, and [Result] is our [/title] command or our [/scoreboard set] (Shown later).

This translates to "Execute as every player, if the block right bellow each player (Here, @s refers to self, or each player previously targeted, in this case, by the [as @a]) is [YourBlock], unless the last stepped block is the one we are looking for now, if so, run [Result] for each player"

(In case you are not familiar with command blocks) Set each command block as the following:

enter image description here

For each block you want to track, we will need two [Results]:

/execute <If statement> run title <your title command>

/execute <If statement> run scoreboard players set @s last_stepped <N>

Let me leave two examples here:

/execute as @a unless score @s last_stepped matches 1 at @s if block ~ ~-1 ~ minecraft:gold_block run title @s title {"text":"Level 1","color":"red"}
/execute as @a unless score @s last_stepped matches 1 at @s if block ~ ~-1 ~ minecraft:gold_block run scoreboard players set @s last_stepped 1

/execute as @a unless score @s last_stepped matches 2 at @s if block ~ ~-1 ~ minecraft:iron_block run title @s title {"text":"Level 2","color":"red"}
/execute as @a unless score @s last_stepped matches 2 at @s if block ~ ~-1 ~ minecraft:iron_block run scoreboard players set @s last_stepped 2

For the use of the OP:

/execute as @a unless score @s last_stepped matches 1 at @s if block ~ ~-1 ~ minecraft:polished_diorite run title @s title {"text":"Level 1","color":"red"}
/execute as @a at @s if block ~ ~-1 ~ minecraft:polished_diorite run scoreboard players set @s last_stepped 1
/execute as @a at @s if block ~ ~-1 ~ minecraft:red_carpet run scoreboard players set @s last_stepped 2