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.
Try this if you are in 1.13:
/execute as @p if block -712 58 -1887 minecraft:air run setblock -729 56 -1884 minecraft:redstone_block replace
In 1.12.2 I tried:
execute @p ~ ~ ~ detect -712 58 -1887 air 0 setblock -729 56 -1884 minecraft:redstone_block
and it worked. It only returned that message if there was already a redstone block there. Did you check if there was one or just check the command block?
BTW it worked when I used minecraft:air
instead of air as well.
Best Answer
If you set the repeating command block to
Needs Redstone
you can prepare it with a Redstone block next to it, you can then add another chain command block that is also set toContitional
and usesetblock
to replace the Redstone block with air.