Updated:

Get a command block

Command blocks are not in the creative inventory. Use /give @s minecraft:command_block, in creative mode with operator rights. On a server, also set enable-command-block=true in server.properties. Other command-only items are listed in the hidden items guide.

Start with one block

Place an Impulse block set to Needs Redstone, type say test and put a button on it. If "test" appears in chat, the block, the redstone input and the command path all work.

The three block types

Get a command working in Impulse first, then move it to Repeat or a chain. The command block types guide goes deeper.

Why it works in chat but not in the block

Relative coordinates change their anchor. In chat, ~ ~ ~ is your position. In a command block it is the block's position.

/summon minecraft:zombie ~ ~1 ~

In a command block, this zombie appears on top of the block, not above you. To use a player's position, wrap it in /execute at:

/execute at @p run summon minecraft:zombie ~ ~1 ~

See the execute guide for as and at.

What each setting means

SettingMeaningUse it for
Needs RedstoneRuns only while it gets a redstone signalButtons and levers
Always ActiveRuns without a signalRepeat blocks that keep watching, and every chain block after the first
UnconditionalRuns whatever happened beforeNormal steps
ConditionalRuns only if the block behind it succeededGive the item only if the payment worked

Set chain blocks after the first one to Always Active. If they stay on Needs Redstone, they do nothing even when their turn comes.

Building a chain

Place chain blocks so each arrow points into the next block. A conditional block needs the block behind it to succeed, so if the chain stops halfway, make the blocks unconditional for a moment and check the order with say.

Debug in this order

  1. Run the command in chat.
  2. Put it in an Impulse block and run it once.
  3. Check that relative coordinates point where you expect.
  4. Narrow the selector before switching to Repeat.
  5. Add chain blocks one at a time and test after each.

Keep Repeat blocks light

A Repeat block runs 20 times a second, so a wide @e can slow the game. Filter by type and distance, such as @e[type=minecraft:zombie,distance=..20] (see target selectors), and wire a lever so you can switch it off. Never put destructive commands like /kill or /fill into a Repeat block until you have tested them once.

Common questions

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.