Minecraft – Target in Minecraft problem

minecraft-commandsminecraft-java-edition

what I'm asking here will be something that I didn't knew how to solve since I use commands blocks. When I target a player, there is @p, @a, @e, and @r. But what I wanted was that the command block target the player that was, like, the nearest, and then continue to target this one until we say to stop.

What I wrote above is my question; if it requires to use the /scoreboard command, please tell me how to use it.

Thank

Best Answer

You must label the closest player first and then perform actions upon the player with that label.

For example, the following labels the closest living player:

/scoreboard players tag @p add closest

Or if you want to include dead players in selection, you must use @a:

/scoreboard players tag @a[c=1] add closest

Then you can target players with that tag:

/say @a[tag=closest]

And you would remove the tag when finished:

/scoreboard players tag @a[tag=closest] remove closest