The /execute Command in Minecraft: as, at, if, store and run With Examples for Your Server
/execute is the command that makes every other command useful: it has a command run by each player, at their own spot, only when something is true. It's the backbone of command blocks, datapacks and almost every minigame built without plugins. Here's how to read it, the examples people use most on a server and the usual mistakes.
How to read an /execute
An /execute is a chain of pieces read left to right, and it always ends with run followed by the real command. Each piece changes something before reaching run: who runs it, where, which way they're facing, in which dimension, or whether it runs at all.
execute as @a at @s run summon minecraft:lightning_bolt reads like this: for every player (as @a), from where that player is (at @s), summon a lightning bolt. Without the at @s, every bolt would land where whoever typed the command is, not on each player.
It needs permission level 2: on a server, being op (ops have level 4 unless someone lowered it); the server console and command blocks can always use it. It's a Minecraft Java command; Bedrock has a similar one since 1.19.50, with fewer pieces (no store and no data conditions).
If an old guide tells you execute @a ~ ~ ~ say hello or execute @a ~ ~ ~ detect…, that's the syntax from before 1.13: it no longer works. The same thing today is execute as @a at @s run say hello, and detect became if block (below).
Rather not set it up yourself? Minecraft servers from €6 a month, no contract and a full refund within 72 hours. Or create a free Minecraft server, no card needed.
as and at: who and from where (mistake number one)
as changes WHO runs the command: from there on, @s is each of those entities. It doesn't change the place: the command still comes from where it was.
at changes WHERE: the position, the facing direction and the dimension become those of that entity. It doesn't change who: @s is still the same as before.
That's why they almost always go together and in this order: execute as @a at @s run … («each player, from where they are»). The other way round, at @a as @a, gives something else: each player runs it from the position of each of the others, and with 5 players the command runs 25 times.
The classic mistake: execute as @a run particle minecraft:heart ~ ~2 ~. All the particles appear in the same spot, because ~ ~ ~ is still the place of whoever launched the command. From the server console, that place is the spawn point of the main world, not yours. With at @s before run, every player gets hearts above their head.
if and unless: run only when something is true
if lets the command through only when the condition is true; unless, only when it isn't. You can chain several, and all of them must hold. If an /execute ends in a condition with no run, the command just reports whether it holds (and how many times), and a command block with a comparator next to it turns that into a redstone signal.
if block: which block is at a spot
execute as @a at @s if block ~ ~-1 ~ minecraft:diamond_block run effect give @s minecraft:speed 2 1 true. Gives speed to anyone standing on a diamond block. It's the old detect: ~ ~-1 ~ is the block right under their feet.if entity: whether someone or something is there
execute if entity @a[distance=..5] run …from a command block holds when a player is 5 blocks away or closer.execute unless entity @e[type=minecraft:zombie] run say No zombies leftworks for the end of a wave.if score: points on a scoreboard
execute as @a if score @s deaths matches 10.. run tag @s add veteran. 10.. means «10 or more», ..9 means «9 or less» and 3..5 means «from 3 to 5». Create the objective first withscoreboard objectives add deaths deathCount.if items: what they hold in their hand
execute as @a if items entity @s weapon.mainhand minecraft:diamond_sword run …holds for anyone holding a diamond sword. It exists since 1.20.5; for the other hand it's weapon.offhand and for armour, armor.head, armor.chest, armor.legs or armor.feet.if dimension and if biome: which world or biome they're in
execute as @a at @s if dimension minecraft:the_nether run …only for players in the Nether.execute as @a at @s if biome ~ ~ ~ minecraft:desert run …for players in a desert. Both exist since 1.19.4.
in, positioned, rotated and facing: moving the command
in changes the dimension: execute in minecraft:the_nether run tp @s 0 100 0 takes the player to those coordinates in the Nether, not in the normal world. The names are minecraft:overworld, minecraft:the_nether and minecraft:the_end, and Multiverse worlds have their own.
positioned moves the starting point without changing anything else: execute positioned 0 64 0 run kill @e[type=minecraft:item,distance=..20] removes dropped items within 20 blocks of the centre of the world, whoever is typing. positioned as @p means «at the nearest player's spot».
rotated and facing change where you're looking. With local coordinates (^ ^ ^5 means «5 blocks ahead, in the direction you're facing»), execute as @a at @s anchored eyes run particle minecraft:flame ^ ^ ^3 puts a flame 3 blocks in front of each player's eyes. anchored eyes makes it count from the eyes instead of the feet.
On Paper (what we deliver), game rules belong to each world: execute in minecraft:the_nether run gamerule keep_inventory true changes it only in the Nether. Typed without the in from the console, it changes only the main world.
store: saving a number to a scoreboard
store saves what a command returns. store result saves the number it gives (how many items, how many entities, the value read); store success saves 1 if it worked and 0 if it didn't.
Counting the diamonds each player carries: create the objective with scoreboard objectives add diamonds dummy and then execute as @a store result score @s diamonds run clear @s minecraft:diamond 0. The clear with 0 at the end doesn't remove anything: it only counts, and that number ends up in each player's score.
Counting how many mobs are loaded: execute store result score #total mobs if entity @e[type=!minecraft:player]. #total is a name that belongs to no player: it shows on the scoreboard and doesn't appear in the Tab list. If that number keeps growing, it's a farm or an uncontrolled spawner, and that's usually the cause of the lag.
on and summon: an entity's relatives
on jumps from one entity to a related one: on vehicle (what it rides), on passengers (who rides on it), on owner (the owner of a wolf or a cat), on attacker (whoever hit it recently), on leasher (whoever holds its lead). execute as @e[type=minecraft:wolf] on owner run … runs the command as the owner of each tamed wolf.
summon summons an entity and carries on with it as @s: execute summon minecraft:armor_stand run tag @s add marker creates an armour stand and tags it in the same step. Both exist since 1.19.4.
Ready-made examples for your server
Safe zone at spawn
In a repeating command block, set to always active:
execute positioned 0 64 0 as @a[distance=..30] run effect give @s minecraft:resistance 2 4 true. Replace 0 64 0 with the centre of your spawn and 30 with the radius. If all you want is that blocks can't be broken, spawn protection already does that without commands.Warning when entering the Nether
With a tag so it only shows once:
execute as @a[tag=!warned] at @s if dimension minecraft:the_nether run tellraw @s {"text":"Careful: you can't sleep here","color":"red"}and, in the next block of the chain,tag @a add warned. Coloured and clickable texts are written for you by the NapNodes tellraw generator.Slime block trampoline
execute as @a at @s if block ~ ~-1 ~ minecraft:slime_block run effect give @s minecraft:jump_boost 1 4 true. Gives Jump Boost V while standing on slime (the 4 is the level minus one).Reward for time played
Create
scoreboard objectives add minutes dummyand, every minute (from a function or a redstone clock),execute as @a run scoreboard players add @s minutes 1. In the same function, right after,execute as @a[scores={minutes=60}] run give @s minecraft:diamond 1gives a diamond after an hour of play, only once (a minute later they already have 61). In a repeating command block it would give it 20 times a second for that whole minute.
Keeping it from causing lag
An /execute in a repeating command block runs 20 times a second. execute as @e at @s run … goes through ALL loaded entities every tick: with farms, dropped items and mobs, that's thousands of times a second, and the whole server feels it.
What helps most: filter in the selector, not afterwards (@e[type=minecraft:zombie,distance=..50] rather than @e followed by an if), use @a instead of @e when you only care about players, add limit=1 when one is enough and, if something doesn't need to happen every tick, move it to a function called every second or to a slower clock.
To check, /tick query (since 1.20.3) or Paper's /mspt tell you how long each tick takes: it has to be under 50 ms. If it jumps when you place a command block, you know who it is. The spark guide shows you which command or plugin eats the most.
Common problems
«Unknown or incomplete command» or «Incorrect argument for command»: it's the pre-1.13 syntax (execute @a ~ ~ ~), the run before the final command is missing, or there's a typo in a name. Block, item and entity names are in English and lowercase (minecraft:diamond_block); the error tells you where it got lost, marked with <--[HERE].
Nothing happens and there's no error: the condition isn't met. Remove the run and everything after it and launch the rest: it will say «Test passed» or «Test failed». The most common causes: the if block checks the wrong block (~ ~ ~ is the block at your feet, not the one below) or the at @s is missing.
The command works in the console but not in a command block: command blocks are switched off on your server (how to turn them on is below), or @s is nobody, because in a command block @s is the block itself and not a player: use as @p or as @a.
A plugin command after run: on Paper it usually works, but many plugins expect a player or the console to type it and don't know what to do with another source. If it errors, try the game's normal command or whatever the plugin offers.
On NapNodes
Minecraft servers are delivered on Paper in the latest version, so everything in this guide works as is. To try an /execute, type it in the server's «Console» in the panel, without the slash: remember that from there the place is the world spawn point, so you'll almost always want an as @a at @s in front. To use it from the game, make yourself op by typing op YourName in the same «Console».
Command blocks are turned on in «Settings» → «Game rules» → «Commands and messages» → «Enable Command Blocks», with the server running, and in the world, the Nether and the End at once. Since 1.21.9 they're no longer turned on in server.properties. If you prefer functions, a datapack is uploaded in «Files» to the world's datapacks folder (the datapacks guide explains it).
In the panel's «Performance» you see your server's CPU graphs: if they climb when you add a repeating command block, check its selectors. The RAM and CPU of your plan are yours alone, with no overselling, so what you see there is what your world uses and not another customer's.
Frequently asked questions
How do you use the /execute command in Minecraft?
You chain pieces and end with run plus the real command: execute as @a at @s run summon minecraft:lightning_bolt has every player, from where they are, summon a lightning bolt. as changes who runs it, at from where, and if or unless add conditions.
What's the difference between as and at in /execute?
as changes who runs the command (@s becomes that entity) and at changes the place, the dimension and the facing direction. For something to happen at each player's position you need both: execute as @a at @s run …
How do I write the old execute detect?
With if block: execute @a ~ ~ ~ detect ~ ~-1 ~ diamond_block 0 say hello is today execute as @a at @s if block ~ ~-1 ~ minecraft:diamond_block run say hello. The old syntax stopped working in 1.13.
How do I detect what a player is holding?
Since 1.20.5, with if items: execute as @a if items entity @s weapon.mainhand minecraft:diamond_sword run … holds for anyone holding a diamond sword.
Is there /execute in Bedrock?
Yes, with the same shape (as, at, if, run) since 1.19.50, but with fewer pieces: it has no store and none of Java's data conditions.
Shall we set it up for you?
Minecraft servers from €6 a month, never oversold: your RAM is yours alone. And if you'd rather start without paying, there's a free Minecraft server, no card needed.
Your server doesn't show up in the game? Check it in a second with “Is my Minecraft server online?”: players, version and MOTD, free. Need a player's UUID for the whitelist or permissions? Look it up here. Handing out enchanted items? Build the command with the /give generator.
Free tool: /tellraw and /title generator. Messages with colours, bold and click actions in chat or across the screen: the /tellraw or /title command ready to copy.
Keep reading
- The /tp Command in Minecraft: to Coordinates, to a Player, to Another Dimension and to Any Structure
How to use /tp in Minecraft: go to coordinates or a player, bring everyone, go to the Nether and find villages or biomes with /locate without landing in a wall.
- The /effect Command in Minecraft: Give Effects, Make Them Infinite and Clear Them
How to use /effect in Minecraft: infinite night vision, speed or strength at any level, no particles, clearing effects and the list of all 41 effect IDs.
- How to Run a UHC on Your Minecraft Server with Friends (No Plugins)
Run a UHC or Hunger Games on your Minecraft server with commands only: no regeneration, a shrinking border, teams, PvP on a timer and spectators.