< close page >

Bedrock

Tutorial 2 of 6

You are viewing the Target Selectors tutorial

YouTubeLinkLinkTwitterLink

Tutorial Selection

Command Block Basics
Target Selectors
Understanding Syntax
Basic Commands
Advanced Commands
Command Systems

Tutorial Contents

What are Target Selectors?

A "target selector" is the intended target for the command you're trying to run. Commands that require a target selector are asking for the entity you intend to run the command as, from or on. This tutorial will discuss everything from the different target selectors to each individual target selector filter available.

Note: There is a lot of information throughout this tutorial, if you'd like to search through it quicker, there are some methods that you can use. If you're using a computer, you can search for information using [Ctrl] + [F]. For mobile devices, some browsers have a "Find in page" or "Find on page" option (usually located in the top right).

If at any point during this tutorial, you see information contained in less than and greater than symbols, it is a space in which you must enter that information. For example, if you see <username>, you'd replace that with the username in the command. The symbols themselves should also be replaced and not included in your command.

Basic Target Selection

The most basic form of target selection is using a player's username/gamertag instead of a selector. When inputting a username as opposed to a selector, you do not need to use the @ symbol. If the username you are trying to input as the target contains spaces, you'll need to use quotation marks. Some examples:


Aside from using a username/gamertag as the target, you may use what is called a "target selector". Information regarding these are below.


Selecting the nearest player (@p)

The "@p" target selector will select the nearest player entity based on where or who the command is executed from. If the command is executed from a player, the closest player will always be the player who the command is executed from. If the command is executed from a location, the closest player to that location will become the target of the command. In Bedrock Edition, the "@p" target selector will only select players who are alive. Here is an example use of this selector:

Note: If there are multiple users detected as the "nearest player" (multiple players are at the exact same distance from the command executor or command's execution point), the player who joined most recently will be selected.


Selecting a random player (@r)

The "@r" target selector will select a random player entity. In Bedrock Edition, the "@r" target selector can be used for non-player entities, if they're specified using a target selector filter such as type or family. Additionally, in Bedrock Edition, the "@r" target selector will only select players who are alive. Here are some example uses of this selector:


Selecting all players (@a)

The "@a" target selector will select ALL player entities. This target selector will select players regardless of whether or not they are dead. This selector will target everyone unless target selector filters are used. Here are some example uses of this selector:


Selecting all entities (@e)

The "@e" target selector will select ALL entities (this includes players) in any rendered chunks. This selector will target every entity unless target selector filters are used. Here are some example uses of this selector:


Selecting the command executor (@s)

The "@s" target selector will select the command executor. The command executor is the entity or command block that the command is run as but not from. If a command uses the "@s" target selector in a command block and the command doesn't specifically run as an entity, the "command executor" is the command block. Here is an example use of this selector:


Selecting the NPC command executor (@initiator)

The "@initiator" target selector is used only inside of an NPC GUI. This target selector can not be used by commands run as the player or a command block. The "@initiator" target selector will target the player who presses a button inside of an NPC GUI. Here are some example uses of this selector:

Positional Target Selector Filters

Positional target selector filters can be used to narrow down target selection based on location. The types of positional target selector filters include coordinates (x, y and z), distance (otherwise referred to as radius) and volume (otherwise referred to as difference values, distance values or d-values). Positional target selector filters can not be duplicated, they may only be entered once per target selector.

Coordinates (x, y, z)

A "coordinate" positional target selector filter will define a starting position in the target selector. To define a starting position using "coordinate" position target selector filters, you would enter an x-axis, y-axis or z-axis value. Please note that coordinate filters are double variables. Coordinate values can be found in the top left corner of your screen once enabled through commands (using /gamerule) or in the game settings menu. Coordinates are shown in the order of x, y and z and start at an entity's feet, not head. Distance and volume positional target selector filters as well as the count target selector filter will use the coordinate filters as a start position. You would type the coordinate filters as x=<value>, y=<value> and z=<value>. Tilde notation (~) may also be used with coordinate filters but caret notation (^) may not. Coordinate target selector filters may not be duplicated in the same target selector.

Note: In Bedrock Edition, if a user inputs an integer value, the value will automatically be centre-corrected; this means that a value such as x=0 would become x=0.5.

Examples

Distance (r, rm)

A "distance" positional target selector filter will define a radius utilising a starting position in the target selector. To define a radius using "distance" positional target selector filters, you would enter an unsigned integer value to set either a radius maximum (r), radius minimum (rm) or both. Both the radius maximum (r) and radius minimum (rm) target selector filters will default to using the command executor's position or the command execution point as the starting position. The starting position can be changed using coordinate positional target selector filters. You would type the distance filters as r=<value> and rm=<value>. Neither of these distance filters can be duplicated in the same target selector. Setting a radius maximum (r) will target all of the specified entities (dependant on other target selector filters) within a spherical radius of the given unsigned integer value, centred from the target selector's starting position. Setting a radius minimum (rm) will exclude all of the specified entities (again, dependant on other target selector filters) within a spherical radius of the given unsigned integer value, which is also centred from the target selector's starting position.

Examples

Image Example

Image shows an example of the r and rm target selector arguments in use.

Volume (dx, dy, dz)

A "volume" positional target selector filter (commonly referred to as "difference values", "distance values" or "d-values") will define an area by volume using the starting position in the target selector. To define a volume utilising a "volume" positional target selector filter, you would enter double values (decimals) or integer values (whole numbers). Just like when calculating volume with anything else, Minecraft will calculate the volumetric area using the formula width x height x length. You may use the "volume" positional target selector filters to create a three-dimensional space in which you can detect entities. The volumetric area will be created based on the distance you specify with your values and the starting position given in the target selector. The starting position can be modified using coordinate positional target selector filters. You would type the volume filters as dx=<value>, dy=<value> and dz=<value>. None of these volume filters can be duplicated in the same target selector. It should be noted that volumetric values start at 0, resulting in the following formula being used to calculate the value total length of area on axis - 1. If coordinate positional target selector filters are not specified, the command will utilise the default starting position, this will be the command executor's position or the command execution point unless specified otherwise. Volumetric values will accept any type of double or integer value, this includes positive and negative values. Any volumetric values left out will default to a value of 0.

Note: In Bedrock Edition, volumetric values require the entity's feet to be inside of the area to select the target.

Common Misinterpretation: Volumetric values are not a second pair of coordinates, they're distance values.

Examples

Image Examples

Image shows a graph explaining how the dx, dy and dz target selector arguments interact with the x, y and z arguments.

Score & Tag Target Selector Filters

Score target selector filters can be used to narrow down target selection based on an entity's score in a scoreboard objective. Tag target selector filters can be used to narrow down target selection based on the tags that an entity has. Using the scores and tag filters is the way to do this. Score arguments can't be duplicated, which means they can't be entered twice in the same target selector. You may test for multiple scores in the same filter, however. Tag target selector filters can be duplicated, allowing the choice of targeting entities with multiple tags.

Scores

A "score" target selector filter will narrow down the target selection based on score values that an entity has in one or more scoreboard objectives. To define the score target selector filter, you would need to input signed integer values (whole numbers), this is because scoreboard objectives only support integers and not double values (decimals). Utilising a score target selector filter will allow you to select targets that meet a specific score, targets that have a score greater than or less than the specified score or targets that have a score between a set range. The score selector filter can't be duplicated inside of the same target selector, though you may check multiple score objectives at once in the same filter. You would type the score target selector filter with scores={<objective>=<value>}. You can edit this to check for multiple scores by typing scores={<objective>=<value>,<objective>=<value>}.

Note: In Bedrock Edition, you may test if scores do not equal the set value parameters. To do this, you would need to input an exclamation mark. You'd do this by typing scores=!{<objective>=<value>}.

Common Mistake: Scoreboard objective names are case-sensitive, meaning a scoreboard objective called "money" is unique to that of one called "Money".

Examples using specific score values

Examples using greater than score values

Examples using less than score values

Examples using score values between a range

Tag

A "tag" target selector filter will narrow down the target selection based on the tags than an entity has. Tags should be a single string of characters with no spaces. If you'd like a tag to have multiple words in it, you can use the snake case naming convention (e.g. two_words or this_tag_has_lots_of_words). To enter a tag target selector filter you would write tag=<string>. You may use exclusion to check if an entity does not have a tag, to do this you would add an exclamation mark, like so: tag=!<string>.

Note: You may test if an entity has multiple tags. To do this, you would need to duplicate the tag target selector filter in the target selector. You'd do this by typing tag=<string>,tag=<string>. The entity needs to have both tags in cases like this, not one or the other. You may also use exclusion when checking for multiple tags.

Common Mistake: Tag names are case-sensitive, meaning a tag called "zombie" is unique to that of one called "Zombie".

Examples

Entity Target Selector Filters

Name target selector filters can be used to narrow down target selection based on an entity's name. Type target selection filters can be used to narrow down target selection based on a type of entity. Family target selection filters can be used to narrow down target selection based on the family an entity belongs to. You would use name, type and family filters to do this. An entity can not have more than one name or type and therefore these filters may not be entered twice in the same target selector. The family target selector filter may be duplicated to check if an entity does or does not belong in multiple families.

Anything above has been updated. Anything below this message has yet to be updated. This message is here to signify the progress made on the tutorial pages refresh project taking place and will be removed after the tutorial has been completely updated. If you'd like to help, please leave feedback via the xSnipez Games Discord server.

Name

This target selector argument filters target selection based on entity names. This argument only allows duplication when testing for inequality. This means you may only duplicate the argument when you are excluding name arguments using the !. Any duplicate arguments testing for equality will cause the target selection to fail. To enter this target selector argument you would write [name=<string>] (you can read more about strings here). To write an excluded name argument, you'd write [name=!<string>]. Names that contain spaces in them must be written inside quotation marks. "Name" arguments are not case-sensitive (meaning "player" is the same as "Player").


Example uses of the name argument

Teleport all players with the name "player" to yourself:

/tp @a[name=player] @s

Apply the Speed III effect to all players for 25 seconds who are not named "player":

/effect @a[name=!player] speed 25 2

Kill all entities that are named "delete me":

/kill @e[name="delete me"]

Type

This target selector argument filters target selection based on entity types. This argument only allows duplication when testing for inequality. This means you may only duplicate the argument when you are excluding type arguments using the !. Any duplicate arguments testing for equality will cause the target selection to fail. To enter this target selector argument you would write [type=<string>] (you can read more about strings here). To write an excluded "type" argument, you'd write [type=!<string>]. Below is a full list of every "type" argument as of Minecraft Bedrock version 1.20.41:


List of every type argument

allay, area_effect_cloud, armor_stand, arrow, axolotl, bat, bee, blaze, boat, camel, cat, cave_spider, chest_boat, chicken, cod, command_block_minecart, cow, creeper, dolphin, donkey, dragon_fireball, drowned, egg, elder_guardian, elder_guardian_ghost, ender_crystal, ender_dragon, ender_pearl, enderman, endermite, evocation_fang, evocation_illager, falling_block, fireball, fireworks_rocket, fishing_hook, fox, frog, ghast, glow_squid, goat, guardian, hoglin, hopper_minecart, horse, husk, iron_golem, item, leash_knot, lightning_bolt, lingering_potion, llama, llama_spit, magma_cube, minecart, npc, ocelot, painting, panda, parrot, phantom, pig, piglin, piglin_brute, pillager, player, polar_bear, pufferfish, rabbit, ravager, salmon, sheep, shulker, shulker_bullet, silverfish, skeleton, skeleton_horse, slime, small_fireball, sniffer, snow_golem, snowball, spider, splash_potion, squid, stray, strider, tadpole, thrown_trident, tnt, tnt_minecart, trader_llama, tropicalfish, turtle, vex, villager, villager_v2, vindicator, wandering_trader, warden, witch, wither, wither_skeleton, wither_skull, wither_skull_dangerous, wolf, xp_bottle, xp_orb, zoglin, zombie, zombie_horse, zombie_pigman, zombie_villager, zombie_villager_v2


Example uses of the type argument

Teleport all player types to coordinates 0, 0, 0:

/tp @a[type=player] 0 0 0

Apply the Jump Boost II effect to all zombie types for 15 seconds:

/effect @e[type=zombie] jump_boost 15 1

Kill all entities that are not armour stands and not skeletons:

/kill @e[type=!armor_stand,type=!skeleton]

Family

This target selector argument filters target selection based on an entity's family. This argument allows duplication. If the argument is duplicated, all arguments must be met for the target selection to be met. To enter this target selector argument you would write [family=<string>] (you can read more about strings here). You may exclude family arguments by using the !. You'd write an excluded family argument as [family=!<string>]. You may see a full list of families here.


Example uses of the family argument

Teleport all entities with the family "arthropods" to 0, 0, 0:

/tp @e[family=arthropod] 0 0 0

Apply the Instant Health I effect to all non-hostile mobs for 1 second:

/effect @e[family=mob,family=!monster] instant_health 1 0

Kill all hostile entities except the undead:

/kill @e[family=monster,family=!undead]

Entity Data Arguments

A vertical or horizontal target selector argument will narrow down the target selection using an entity's rotation following the axis. An item target selector argument will narrow down the target selection using items an entity has. This can be done by using vertical rotation, horizontal rotation and item arguments. These arguments are explained below. None of these target selector arguments can be duplicated, which means they can't be entered twice in the same target selection.

This section is very technical. Think it can be improved? Please leave a suggestion on the xSnipez Games Discord Server.

Vertical Rotation (pitch) (rx, rxm)

These target selector arguments set a maximum vertical rotation (rx) and minimum vertical rotation (rxm) for the target selector. To enter these target selector arguments, you would type them as rx=<value> and rxm=<value>. These values have a maximum of 90 (looking directly down) and a minimum of -90 (looking directly up). Vertical rotation in Minecraft is based on how high the player is looking on the y-axis. You can use maximum vertical rotation (rx) and minimum vertical rotation (rxm) to select targets based on where they are looking on the y-axis. These arguments can not be inverted using !.


Example uses of the rx and rxm arguments

Teleport all players looking directly at the horizon to 0, 0, 0:

/tp @a[rx=0,rxm=0] 0 0 0

Apply the Instant Damage II effect to all players looking between 30° and 60° for 1 second:

/effect @a[rx=60,rxm=30] instant_damage 1 1

Kill all entities looking at or above the horizon:

/kill @e[rx=0]

Horizontal Rotation (yaw) (ry, rym)

These target selector arguments set a maximum horizontal rotation (ry) and minimum horizontal rotation (rym) for the target selector. To enter these target selector arguments, you would type them as ry=<value> and rym=<value>. These values have a maximum of 180 (north) and a minimum of 0 (south). This argument is a little confusing, so I'll give a few directions and their rotation values: North (N) (180 or -180), North East (NE) (-135), East (E) (-90), South East (SE) (-45), South (S) (0), South West (SW) (45), West (W) (90) and North West (NW) (135). Horizontal rotation in Minecraft is based on which direction the player is looking on the x-axis. You can use maximum horizontal rotation (ry) and minimum horizontal rotation (rym) to select targets based on where they are looking on the x-axis. These arguments can not be inverted using !.


Example uses of the ry and rym arguments

Teleport all players facing south to 0, 0, 0:

/tp @a[ry=0,rym=0] 0 0 0

Apply the Instant Damage II effect to all players in the 90° angle between east and south for 1 second:

/effect @a[ry=0,rym=-90] instant_damage 1 1

Kill all entities that are not facing east in the 180° angle between south and north:

/kill @e[ry=180,rym=0]

Item (hasitem)

This target selector argument filters target selection based on items an entity has. This argument can not be duplicated, but you may specify multiple items in the same argument. To enter this argument with one item, you would write [hasitem={<argument>=<value>,<argument>=<value>,...}]. To enter this argument with multiple items, you would write [hasitem=[{<argument>=<value>},{<argument>=<value>}]]. The hasitem target selector argument has other arguments, or as I like to call them, "sub-arguments". These "sub-arguments" are data, item, location, quantity and slot.


Valid location sub-arguments for the hasitem argument

slot.armor, slot.armor.chest, slot.armor.feet, slot.armor.head, slot.armor.legs, slot.chest, slot.enderchest, slot.equippable, slot.hotbar, slot.inventory, slot.saddle, slot.weapon.mainhand, slot.weapon.offhand


Syntax of the quantity sub-argument for the hasitem argument


Syntax of the slot sub-argument for the hasitem argument


Example uses of the hasitem argument

Teleport all players who have a diamond to 0, 0, 0:

/tp @a[hasitem={item=diamond}] 0 0 0

Teleport all players who have exactly 12 diamonds to 12, 0, -29:

/tp @a[hasitem={item=diamond,quantity=12}] 12 0 -29

Teleport all players who have exactly 32 diamonds or higher to 234, 0, -3:

/tp @a[hasitem={item=diamond,quantity=32..}] 234 0 -3

Teleport all players who have exactly 16 diamonds or less to 34, 0, -13:

/tp @a[hasitem={item=diamond,quantity=..16}] 34 0 -13

Teleport all players who have between 16 and 32 diamonds to 96, 0, 1:

/tp @a[hasitem={item=diamond,quantity=16..32}] 96 0 1

Apply the Haste II effect to all players that are holding a diamond pickaxe in their main hand for 5 seconds:

/effect @a[hasitem={item=diamond_pickaxe,location=slot.weapon.mainhand] haste 5 1

Kill all entities that don't have exactly 64 iron ingots:

/kill @e[hasitem={item=iron_ingot,quantity=!64}]

Kill all entities that have exactly 32 gold ingots or higher in the 5th slot of their hotbar:

/kill @e[hasitem={item=gold_ingot,quantity=32,location=slot.hotbar,slot=4}]

Kill all entities that have between 27 and 49 creeper skulls in their inventory between slots 10 and 18:

/kill @e[hasitem={item=skull,data=4,quantity=27..49,location=slot.inventory,slot=9..18}]

Kill all entities that have between 11 and 52 creeper skulls in their inventory between slots 1 and 9 and don't have exactly 34 or higher wither skeleton skulls in their hotbar between slots 2 and 8:

/kill @e[hasitem=[{item=skull,data=4,quantity=11..52,location=slot.inventory,slot=0..8},{item=skull,data=1,quantity=!34..,location=slot.hotbar,slot=1..7}]

Player Data Arguments

An experience level target selector argument will narrow down the target selection using a player's experience level. A game mode target selector argument will narrow down the target selection using a player's current game mode. A permission target selector argument will narrow down the target selection using a player's current permissions. This can be done by using experience level, gamemode and permission arguments. These arguments are explained below. Experience-level and permission arguments can not be duplicated. Only arguments testing for inequality in gamemode arguments can be duplicated, gamemode arguments testing for equality can not.

Experience Level (l, lm)

These target selector argument filters target selection based on a player's experience levels. This argument can not be duplicated, meaning it can only be used once in any given target selection. The arguments set a level maximum (l) and level minimum (lm) to search for. To enter these target selector arguments, you would type them as l=<value> and lm=<value>. It is important to note that only integer values are accepted. It should be noted that these arguments are only specific to player entities, and will naturally filter out all non-player types.


Example uses of the l and lm arguments

Teleport all players who have exactly 5 experience levels to 0, 0, 0:

/tp @a[l=5,lm=5] 0 0 0

Apply the Regeneration IV effect to all players who have between 0 and 10 experience levels for 30 seconds:

/effect @a[l=10,lm=0] regeneration 30 3

Kill all entities that have exactly 30 experience levels:

/kill @e[l=30,lm=30]

Game Mode (m)

This target selector argument filters target selection based on a player's current gamemode. This argument can only be duplicated if testing for inequality. This means you may only duplicate the argument when you are excluding m arguments using the !. Any duplicate arguments testing for equality will cause the target selection to fail. The argument checks for the player's current gamemode. To enter this target selector argument, you would type it as m=<value> or m=<string>. It is important to note that only integer and string values are accepted. There are only four gamemode types, these are survival (survival, s or 0 are accepted), creative (creative, c or 1 are accepted), adventure (adventure, a or 2 are accepted) and spectator (only spectator is accepted). 


Example uses of the m argument

Teleport all players in creative to 0, 0, 0:

/tp @a[m=creative] 0 0 0

Apply the Regeneration III effect to all players who are in the adventure gamemode for 20 seconds:

/effect @a[m=a] regeneration 20 2

Kill all players in the survival gamemode:

/kill @e[m=0]

Permission (haspermission)

This target selector argument filters target selection based on a player's permissions. This argument can not be duplicated, meaning it can only be used once in any given target selection. The argument checks for the player's current camera and movement permissions. To enter this target selector argument, you would type it as [haspermission={<permission>=<state>}]. A state can be entered as enabled or disabled. A permission argument can only be camera or movement.


Example uses of the haspermission argument

Teleport all players who have no camera movement permissions to 0, 0, 0:

/tp @a[haspermission={camera=disabled}] 0 0 0

Teleport all players who have movement permissions to 0, 0, 0:

/tp @a[haspermission={movement=enabled}] 0 0 0

Kick all players who have no camera or movement permissions:

/kick @a[haspermission={camera=disabled,movement=disabled}]

Trait Arguments

A count target selector argument will narrow down the target selection using a value limit. This can be done by using a count argument. This argument is explained below. This argument can't be duplicated.

Count (c)

This target selector argument filters target selection based on a set limit. This argument can not be duplicated, meaning it can only be used once in any given target selection. The argument limits the number of entities the command effects. To enter this target selector argument, you would type it as c=<value>. It is important to note that only integer values are accepted.


Example uses of the c argument

Teleport 2 living, randomly chosen players to 0, 0, 0:

/tp @r[c=2] 0 0 0

Teleport the nearest 3 players from the command's executor to 0, 0, 0:

/tp @p[c=3] 0 0 0

Teleport the furthest 3 players from the command's executor to 0, 0, 0:

/tp @p[c=-3] 0 0 0

Kills 5 entities:

/kill @e[c=5]