Minecraft – How to make a command block say something only once in Minecraft

minecraft-commandsminecraft-java-edition

I am creating an adventure map and needed to know how to make a command block only say something once in the chat menu.

PS: I would like this to be under my floor because it is supposed to be an NPC talking. I also need to know how to set the radius so the text only appears when close enough to the command block.

Best Answer

Here's a compacted device that will do what you want. Two command blocks are necessary; one for the player detector, and one for the message. I've labeled the blocks with example commands to use.

Explanation:

The six blocks in the top-left form a basic clock, which repeatedly triggers the testfor command. (The faster the clock, the quicker the detection, but higher CPU cost ("server lag") to run it.)

The torch of the clock activates the testfor command block at left, and (towards the right) its output is detected by the comparator, amplified by the repeater, and goes in to the command block at the right to trigger the message.

The signal then also goes to the left-pointing repeater and into the block holding the torch. This stops the clock, so that the player test will not run again. This means that left command block's output remains on, so the right command block won't say anything again since it's waiting to see the signal turn on.

Then, to reset the mechanism (after testing, say), push the button at the bottom, which reruns the player test and thus reenables the clock, while you are out of range. (It'll need to be longer than shown to be actually out of range.)

The redstone dust on top of the command block is not actually necessary (it just looks better), and if you omit it the entire mechanism is only one block high so it'll fit easier under a floor.


If you want to do the same kind of thing for something other than a message, you can just omit the command block on the right and use the redstone output for whatever you want (as long as it also loops back to stop the clock).


By the way: as a matter of game design, I would recommend that you not do exactly this, unless the message is purely flavor and not an important hint. Don't make an NPC say something only once, because the player could easily miss it the first time around, or assume they could get the information again later since most games do that.

If you simply omit the part which stops the clock (the left-pointing repeater and the wire going to it), then there will be a message each time the player enters the detection range (but not any repetitions while they are in it).

Adding a bit more redstone logic could do things like giving a long message the first time the player is detected and a short one later times.