Minecraft – How to get a command block to say the name of a named mob

minecraft-commandsminecraft-java-edition

I'm making a horse racing track and I want the game to say "[name of horse] has finished the race!" when a horse crosses the finish line. I can get it to say the name of every horse in the area, but not just the one that activates the block.

Best Answer

If you're activating the command block with a pressure plate you could use the @e selector combined with sort=nearest and limit=1 like this:

/tellraw @a ["",{"selector":"@e[sort=nearest,limit=1]"},
{"text":" has finished the race!"}]

If players are riding the horses you could also add type=horse in the selector:

/tellraw @a ["",{"selector":"@e[sort=nearest,limit=1,type=horse]"},
{"text":" has finished the race!"}]