Minecraft Wiki

The Minecraft Wiki is no longer considered as official by Microsoft and therefore several changes are required to be made, including to the wiki's logo. Please read this announcement for more information.

READ MORE

Minecraft Wiki
Advertisement
Minecraft Wiki

Target selectors are used in commands to select players and entities arbitrarily, without needing to specify an exact player name or a UUID. One or more entities can be selected with a target selector variable, and targets can be filtered from the selection based on certain criteria using the target selector arguments.

For example, to change the game mode of all players to Creative mode, instead of specifying each of them by name individually:

/gamemode creative @a

Target selector variables[]

Summary of target selector variables
Variable Function
@p nearest player
@r random player
@a all players
@e all entities
@s entity executing the command
@c the player's agent‌[Education Edition only]
@v all agents‌[Education Edition only]
@initiator the player who clicks an NPC dialogue button‌[Bedrock Edition only]

A target selector variable identifies the broad category of targets to select. There are five (six in Bedrock Edition, seven in Education Edition) variables:

@p
Targets the nearest player from the command's execution. If there are multiple nearest players, caused by them being precisely the same distance away, the player who most recently joined the server is selected.
In Bedrock Edition, @p only targets players who are alive.
@r
Targets a random player.
Bedrock Edition can use @r to target non-player entities via the type selector argument; in Java Edition, to select a random entity, use @e[sort=random] instead.
In Bedrock Edition, @r can only target entities who are alive.
@a
Targets every player, alive or not.
@e
Targets all alive entities (including players) in loaded chunks.
@s
Targets the entity (alive or not) that executed the command. It does not target anything if the command was run by a command block or server console.
@c[Education Edition only]
Target the player's agent only.
@v[Education Edition only]
Target all agents. Works only if more than one agent exists.
@initiator[Bedrock Edition only]
Targets the player who interacts with a button in a JSON NPC dialogue.

Target selector arguments[]

Summary of target selector arguments
Selection by Position
Argument(s) Selection criteria
x, y, z coordinate
distance[JE only]
r, rm[BE only]
distance
dx, dy, dz volume dimensions
Selection by Scoreboard Values
Argument(s) Selection criteria
scores scores
tag tag
team[JE only] team name
Selection by Traits
Argument(s) Selection criteria
limit,sort[JE only]
c[BE only]
limit
level[JE only]
l,lm[BE only]
experience level
gamemode[JE only]
m[BE only]
game mode
name entity name
x_rotation[JE only]
rx,rxm[BE only]
vertical rotation
y_rotation[JE only]
ry,rym[BE only]
horizontal rotation
type entity type
family[BE only] entity family
nbt[JE only] nbt
advancements[JE only] advancements
predicate[JE only] predicate

After a target selector, optional arguments can be used to narrow down the set of targets to a group that also matches certain criteria. When used with @a or @e, arguments narrow down the targets from the full list to a specific group. When used with @p or @r, the nearest or random player is selected from the group. When used with @s, the player using the command is targeted only if they would be in the narrowed group.

Argument-value pairs appear within square brackets after the target selector variable, separated by commas:

@<variable>[<argument>=<value>,<argument>=<value>,...].

In Java Edition, arguments and values are case-sensitive. Spaces are allowed around the brackets, equal signs, and commas, except in Java Edition between the target variable and the first bracket. Commas must be used to separate argument-value pairs.

If there are multiple argument-value pairs, they all must be satisfied to add a potential target to the group. (In other words, they are AND-ed together).

Position arguments
[x=<value>,y=<value>,z=<value>] — Define a position in the world the selector starts at, for use with the distance argument, the volume arguments, or the limit argument. Using these arguments alone will not restrict the entities found, and will only affect the sorting of targets. Cannot duplicate any one of these three arguments.
The positional components are doubles, allowing for values like +12.34, and they are not center-corrected, meaning x=0 is not corrected to x=0.5.
In Bedrock Edition, tilde notation is available for selector argument coordinates.
In Java Edition, these arguments limit the search of entities to the current dimension.
Selecting targets by distance
Filter target selection based on their Euclidean distances from some point, searching for the target's feet (a point at the bottom of the center of their hitbox). If the positional arguments are left undefined, radius is calculated relative to the position of the command's execution. This argument limits the search of entities to the current dimension. Cannot duplicate these arguments.
In Java Edition: [distance=<value>] — Specifies the range of distance. Float ranges are supported to select a specific region. Only unsigned values are allowed.
In Bedrock Edition: [r=<value>] and [rm=<value>] — Specifies the maximum and minimum range to find entities, respectively. Only unsigned values are allowed.
Examples in Java Edition:
  • @e[distance=10] — Target all entities exactly ten blocks away.
  • @e[distance=8..16] — Target all entities more than eight blocks, but less than 16 blocks away (inclusive).
Examples in Bedrock Edition:
  • @e[rm=9,r=10] — Target all entities exactly ten blocks away.
  • @e[rm=8,r=16] — Target all entities from 8 to 16 blocks away.
Selecting targets by volume
[dx=<value>,dy=<value>,dz=<value>] — Filter target selection based on their x-difference, y-difference, and z-difference from some point, as measured by entities' hitboxes in Java Edition or by their feet in Bedrock Edition. Cannot duplicate any one of these three arguments.
This can be interpreted as creating a rectangular volume defined by an initial position (<x>,<y>,<z>) and diagonal vector (<dx>,<dy>,<dz>), then selecting all entities whose hitboxes are at least partially contained by that volume in Java Edition, or whose feet are within that volume in Bedrock Edition. If the positional arguments are left out, the selection is interpreted as originating from the position of the command's execution. Any values are allowed, including signed and fractional numbers.
Note that dx,dy,dz specify signed differences from the given coordinate. They do not specify a separate coordinate, nor do they extend in both the positive and negative directions.
Additionally, when any volume argument is present, the rest are assumed to equal zero unless otherwise defined.
Examples in Java Edition:
  • @e[x=1,dx=4,y=2,dy=5,z=3,dz=6] — Select all entities whose hitbox collides with the block region (1~5, 2~7, 3~9) (or, mathematically speaking, the region that is {(x,y,z)∈R3|x∈[1.0,5.0),y∈[2.0,7.0),z∈[3.0,9.0)}).
  • @e[x=1,y=2,z=3,dx=0,dy=0,dz=0] — Select all entities whose hitbox contains the point (1,2,3).
Examples in Bedrock Edition:
  • @e[x=1,dx=4,y=2,dy=5,z=3,dz=6] — Select all entities whose feet are within the block region (1~5, 2~7, 3~9).
  • @e[x=1,y=2,z=3,dx=0,dy=0,dz=0] — Select all entities whose feet contains the point (1, 2, 3).
It is possible to combine selection by distance and selection by volume, in which case the command select targets only within the overlap of both regions (within a certain radius of the volume's initial point and not outside the defined volume).
Selecting targets by scores
[scores={<objective>=<value>,...}] — Filter target selection based on their scores in the specified objectives. All tested objectives are in a single object, with a list of individual score arguments between braces afterward. The values inside the braces support integer ranges. Cannot duplicate this argument.
  • @e[scores={myscore=10}] — Select all entities with a score in objective myscore of exactly ten.
  • @e[scores={myscore=10..12}] — Select all entities with a score in objective myscore of between ten and 12 (inclusive).
  • @e[scores={myscore=5..}] — Select all entities with a score in objective myscore of five or greater.
  • @e[scores={myscore=..15}] — Select all entities with a score in objective myscore of 15 or less.
  • @e[scores={foo=10,bar=1..5}] — Select all entities with a score in objective foo of exactly ten, and a score in objective bar of between one and five (inclusive).
In Bedrock Edition, ! can be used to invert selection.
  • @e[scores={myscore=!10}] — Exclude any entities with a score in objective myscore of exactly ten.
  • @e[scores={myscore=!10..15}] — Exclude any entities with a score in objective myscore of between ten and 15 (inclusive).
Selecting targets by tag
Filter target selection based on the entity's scoreboard tags. Multiple tag arguments are allowed, and all arguments must be fulfilled for an entity to be selected.
[tag=<string>] — Include only targets with the specified tag.
[tag=!<string>] — Exclude any targets with the specified tag.
[tag=] — Include only targets with exactly zero tags.
[tag=!] — Exclude any targets that have at least one tag.
See also: Commands/tag
Selecting targets by team[Java Edition only]
Filter target selection based on teams. Arguments testing for equality cannot be duplicated, while arguments testing for inequality can.
[team=<teamName>] — Include only targets in the given team.
[team=!<teamName>] — Exclude any targets in the given team.
[team=] — Include only targets not in a team.
[team=!] — Exclude any targets not in a team.
See also: Commands/team
Limiting and sorting target selection
Limit the number of selectable targets for a target selector.
When using the variables @p and @r, this argument defaults to one. Applying the limiting argument to them may increase the number of nearest or random targets selected. When applying this argument to @a or @e, this argument returns only a limited number of targets. Cannot duplicate these arguments.
In Java Edition: [limit=<value>,sort=(nearest|furthest|random|arbitrary)] — Limit the number of targets, and specify selection priority.
  • sort=nearest — Sort by increasing distance. (Default for @p)
  • sort=furthest — Sort by decreasing distance.
  • sort=random — Sort randomly. (Default for @r)
  • sort=arbitrary — Sort by time created. (Default for @e, @a)
In Bedrock Edition: [c=<value>] — Limit the number of targets.
For @p, @a, and @e, [c=<value>] selects only the specified number of targets by increasing distance from the selector's position. When c is negative, it will reverse the order of targeting (for example, @p[c=-1] will target the furthest player). Inverse sorting does not work with @r.
Examples in Java Edition:
  • @a[limit=3,sort=nearest] or @p[limit=3] — Select the nearest three players.
  • @a[limit=4,sort=furthest] — Select the furthest four players.
  • @a[limit=2,sort=random] or @r[limit=2] — Select two players, chosen randomly.
Examples in Bedrock Edition:
  • @a[c=3] — Select the nearest three players.
  • @a[c=-4] — Select the furthest four players.
  • @r[c=2] — Select two living players, chosen randomly.
Selecting targets by experience level
Filter target selection based on the entity's experience levels. This naturally filters out all non-player targets. Cannot duplicate these arguments.
In Java Edition: [level=<value>] — Specifies the range of levels. Integer ranges are supported to select a range of values.
In Bedrock Edition: [l=<value>] and [lm=<value>] — the maximum and minimum level range to search for, respectively.
Examples in Java Edition:
  • @a[level=10] — Select all players who have exactly ten levels.
  • @a[level=8..16] — Select all players who have between eight and 16 levels (inclusive).
Examples in Bedrock Edition:
  • @a[lm=10,l=10] — Select all players who have exactly ten levels.
  • @a[lm=8,l=16] — Select all players who have between eight and 16 levels (inclusive).
Selecting targets by game mode
Filter target selection by game mode. This naturally filters out all non-player targets. Arguments testing for equality cannot be duplicated, while arguments testing for inequality can.
In Java Edition:
[gamemode=<gamemodeName>] — Include only players in the given game mode.
[gamemode=!<gamemodeName>] — Exclude any players in the given game mode.
In Bedrock Edition:
[m=<gamemodeName>] — Include only players in the given game mode.
[m=!<gamemodeName>] — Exclude any players in the given game mode.
Permitted values for <gamemodeName> are spectator,‌[JE only] survival, creative, adventure. In Bedrock Edition, the shorthand values s and 0, c and 1, and a and 2 may be used for Survival mode, Creative mode, and Adventure mode respectively.
Selecting targets by name
Filter target selection by name. Values are strings, so spaces are allowed only if quotes are applied. This cannot be a JSON text compound. Arguments testing for equality cannot be duplicated, while arguments testing for inequality can.
[name=<givenName>] — Include only targets with the given name.
[name=!<givenName>] — Exclude any targets with the given name.
Selecting targets by vertical rotation
Filter target selection based on the entity's rotation along the pitch axis, measured in degrees. Values range from -90 (straight up) to 0 (at the horizon) to +90 (straight down). Cannot duplicate these arguments.
In Java Edition: [x_rotation=<value>] — Specifies the range of x-rotation. Float ranges are supported to select a specific range of angles.
In Bedrock Edition [rx=<value>] and [rxm=<value>] — Specifies the maximum and minimum x-rotation, respectively.
Examples in Java Edition:
  • @e[x_rotation=0] — Select all entities that are looking directly at the horizon.
  • @e[x_rotation=30..60] — Select all entities that are looking between 30° and 60° (inclusive) below the horizon.
  • @e[x_rotation=..0] — Select all entities that are looking at or above the horizon.
Examples in Bedrock Edition:
  • @e[rxm=0,rx=0] — Selects all entities that are looking directly at the horizon.
  • @e[rxm=30,rx=60] — Selects all entities that are looking between 30° and 60° (inclusive) below the horizon.
  • @e[rx=0] — Select all entities that are looking at or above the horizon.
Selecting targets by horizontal rotation
Filter target selection based on the entity's rotation along the yaw axis, measured clockwise in degrees from due south (or the positive Z direction). Values vary from -180 (facing due north) to -90 (facing due east) to 0 (facing due south) to +90 (facing due west) to +180 (facing due north again). Cannot duplicate these arguments.
In Java Edition: [y_rotation=<value>] — Specifies the range of y-rotation. Float Ranges are supported to select a specific range of angles.
In Bedrock Edition: [ry=<value>] and [rym=<value>] — Specifies the maximum and minimum y-rotation values, respectively.
Examples in Java Edition:
  • @e[y_rotation=0] — Select all entities that are facing due south.
  • @e[y_rotation=-90..0] — Select all entities that are facing in the 90° between due east and due south (inclusive).
  • @e[y_rotation=0..180] — Select all entities that are not facing at all east.
Examples in Bedrock Edition:
  • @e[rym=0,ry=0] — Select all entities that are facing due south.
  • @e[rym=-90,ry=0] — Select all entities that are facing in the 90° between due east and due south (inclusive).
  • @e[rym=0,ry=180] — Select all entities that are not facing at all east.
Selecting targets by type
Filter target selection based on the entity's identifier. The given entity type must be a valid entity ID or entity type tag[Java Edition only] used to identify different types of entities internally. The namespace can be left out if the ID is within the minecraft namespace. Entity IDs or tags are case-sensitive. Arguments testing for equality cannot be duplicated, while arguments testing for inequality can.
[type=<entityType>] — Include only targets of the specified entity type or tag.
[type=!<entityType>] — Exclude any targets of the specified entity type or tag.
Selecting targets by family[Bedrock Edition only]
Filter target selection based on the entity's type_family behavior component. Default values used by the vanilla behavior pack include among others more broad terms like mob and inanimate, as well as more specific families like zombie and skeleton, and single-mob families like wandering_trader and creeper. Multiple family arguments are allowed, and all arguments must be fulfilled for an entity to be selected.
[family=<family>] — Include only targets in the specified type family.
[family=!<family>] — Exclude any targets in the specified type family.
  • @e[family=skeleton] — Select all skeletons, wither skeletons and strays.
  • @e[family=mob,family=!monster] — Select all mobs that are not also monsters (so for example cows, chickens, pigs, but not zombies or skeletons).
  • @e[family=monster,family=undead] — Select all monsters that are also undead (that includes monsters like zombies and skeletons, but not creepers or endermen).
Selecting targets by NBT data‌[Java Edition only]
Filter target selection based on the entity's NBT data. The NBT data is written in its SNBT format. Multiple nbt arguments are allowed, and all arguments must be fulfilled for an entity to be selected.
See NBT format#Testing NBT tags for more infomation about this.
Note that this selector argument should be used with care, as accessing NBT data is a heavy process for the CPU.
[nbt=<compoundTag>] — Include only targets with the specified NBT data.
[nbt=!<compoundTag>] — Exclude any targets with the specified NBT data.
  • @a[nbt={OnGround:true}] — Select all players on the ground.
  • @e[type=sheep,nbt={Color:0b}] — Select all sheep that are dyed white.
  • @e[type=item,nbt={Item:{id:"minecraft:slime_ball"}}] — Selects all slime ball item entities.
  • @e[nbt={Tags:[a,b]}] is the same as @e[tag=a,tag=b]. The latter is simpler and reduces CPU load.
Selecting targets by advancements[Java Edition only]
Filter target selection based on the entity's advancements. This naturally filters out all non-player targets. All advancements are in a single object, with a list of individual advancement IDs between the braces afterward. The values are true or false. For advancements with one criterion, testing for that criterion always gives the same results as testing for the advancement. Cannot duplicate this argument.
[advancements={<resource location>=<bool>}] — Include only players with the specified advancements and values.
[advancements={<resource location>={<criteria>=<bool>}}] — Include only players with the specified advancement's criteria.
  • @a[advancements={story/smelt_iron=true}] — Include only players who have completed the advancement minecraft:story/smelt_iron.
  • @a[advancements={story/form_obsidian=false}] — Include only players who haven't completed the advancement minecraft:story/form_obsidian.
  • @a[advancements={story/follow_ender_eye=true}] is the same as @a[advancements={story/follow_ender_eye={in_stronghold=true}}].
Selecting targets by predicate[Java Edition only]
Filter target selection by predicates. The given values must be a valid predicate represented by a resource location. Multiple predicate arguments are allowed, and all arguments must be fulfilled for an entity to be selected.
[predicate=<resource location>] — Include only targets that match the specified predicate.
[predicate=!<resource location>] — Exclude any targets that match the specified predicate.

History[]

Java Edition
1.4.212w32aAdded target selectors.
1.814w02aAdded the @e target selector variable.
14w03aAdded dx, dy, and dz to target selector arguments.
1.916w02aThe m selector now accepts the standard literal gamemode values and abbreviations, such as a and adventure, and these may be negated as well.
1.1116w38aRemoved implicit target selectors (for example, @e[167,28,454]).
Invalid target selectors (such as @e[Type=creeper], @e[asdf=nonexistent], and @e[malformed]) now generate an error instead of being silently ignored.
1.1217w16bAdded the @s target selector variable.
1.1317w45bAdded the nbt target selector argument.
Bedrock Edition
?Aded target selectors
1.17.10beta 1.17.10.22Added the @initiator target selector


Issues[]

Issues relating to "Target selectors" are maintained on the bug tracker. Report issues there.

See also[]

Advertisement