Updated:

Run a function in five seconds

/schedule function mypack:explode 5s

mypack:explode is the function in data/mypack/function/explode.mcfunction (the folder was called functions before 1.21).

Time units

WriteMeaning
20t or 2020 ticks (1 second)
5s5 seconds (100 ticks)
1dOne in-game day (24000 ticks, about 20 minutes)

A repeating loop

# data/mypack/function/loop.mcfunction
effect give @a minecraft:regeneration 3 0 true
schedule function mypack:loop 10s

The function schedules itself again at the end. Run /function mypack:loop once and it repeats every 10 seconds, which is lighter than running something every tick.

Cancel a schedule

/schedule clear mypack:loop

Schedules are saved with the world, so they continue after reloading until you clear them.

replace and append

CommandIf the function is already scheduled
/schedule function mypack:a 5s replaceReplace the old schedule (default)
/schedule function mypack:a 5s appendKeep the old one and add another

Who runs the function

A scheduled function runs as the server, at the world spawn, not as the player who scheduled it. @s and ~ ~ ~ therefore do not point at a player. Use execute as @a at @s run … inside the function to act on each player.

A 3-2-1 countdown

# data/mypack/function/start.mcfunction
title @a title "3"
schedule function mypack:count2 1s
# data/mypack/function/count2.mcfunction
title @a title "2"
schedule function mypack:count1 1s
# data/mypack/function/count1.mcfunction
title @a title "1"
schedule function mypack:go 1s
# data/mypack/function/go.mcfunction
title @a title {"text":"GO!","color":"green"}

Each function shows a number and schedules the next one a second later. Starting the round only needs /function mypack:start.

Delay an action for one player

Because the scheduled function does not know who started it, mark the player with a tag first:

# when the player steps on the trap
tag @s add trapped
schedule function mypack:release 5s
# data/mypack/function/release.mcfunction
tp @a[tag=trapped] 0 64 0
tag @a remove trapped

If a second player triggers it within five seconds, the default replace restarts the timer and both players are released together. For a separate timer per player, count ticks in a scoreboard instead.

Bedrock Edition

Bedrock has /function and /schedule, but its /schedule works differently (it was made to run functions when an area loads) and does not accept the syntax on this page. Counting ticks with a scoreboard is the reliable way to delay actions on Bedrock.

Common problems

Check your command

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