|
| 1 | +--- |
| 2 | +title: Scoreboard-based cooldown |
| 3 | +description: Learn how to create and check cooldown timers using a scoreboard objective. |
| 4 | +version: 26.1 |
| 5 | +--- |
| 6 | + |
| 7 | +# Scoreboard-based cooldown |
| 8 | + |
| 9 | +A scoreboard-based cooldown stores a number on each player (or entity) and |
| 10 | +changes that number over time. |
| 11 | + |
| 12 | +This guide uses a ticking approach, which means the datapack updates the |
| 13 | +cooldown every game tick (20 ticks per second). This works well |
| 14 | +for small projects with only a few cooldowns and short durations. If you track many cooldowns or |
| 15 | +many entities, this negatively affects performance, because the game has to run loads of commands. |
| 16 | + |
| 17 | +In summary, this approach sets a player's score on a scoreboard, and then decreases that score every |
| 18 | +tick. This means that, when we need to do something which requires a cooldown, we can just check the |
| 19 | +score to see if the player has an active cooldown or not. |
| 20 | + |
| 21 | +## What you will create |
| 22 | + |
| 23 | +- A `my_cd` scoreboard objective (this tracks the actual cooldown for each plalyer) |
| 24 | +- A `tick` function (this reduces each player's cooldown score) |
| 25 | +- A `check_cooldown` function (this will be used to check and reset the player's cooldown) |
| 26 | + |
| 27 | +:::tip |
| 28 | + |
| 29 | +To learn how to trigger a function when a player uses an item, see the |
| 30 | +[Right-click menu](/guide/right-click) guide. |
| 31 | + |
| 32 | +::: |
| 33 | + |
| 34 | +### 1. Create the scoreboard objective |
| 35 | + |
| 36 | +In your load function, create a dummy objective named `my_cd`. A dummy |
| 37 | +objective is just a scoreboard which can only be changed by commands. |
| 38 | + |
| 39 | +```mcfunction:load.mcfunction |
| 40 | +scoreboard objectives add my_cd dummy |
| 41 | +``` |
| 42 | + |
| 43 | +### 2. Reduce the score in the `tick` function |
| 44 | + |
| 45 | +In your `tick` function (which runs every tick), reduce the `my_cd` |
| 46 | +score by 1 for any player with `my_cd` ≥ 1. |
| 47 | + |
| 48 | +```mcfunction:tick.mcfunction |
| 49 | +scoreboard players remove @a[scores={my_cd=1..}] my_cd 1 |
| 50 | +``` |
| 51 | + |
| 52 | +:::note |
| 53 | + |
| 54 | +Alternatively, if optimisation is an concern, you can decrease the commands used, and use a `minecraft:tick` advancement so that |
| 55 | +it only runs the command when the player has an active cooldown. To do this, you would need a tick advancement which runs a function - |
| 56 | +in that function, you would decrease the player's score and then revoke the advancement only if their score is 1 or more. This would replace |
| 57 | +the commands in the tick function, and slightly help to minimise lag. |
| 58 | + |
| 59 | +::: |
| 60 | + |
| 61 | +### 3. Create the `check_cooldown` function |
| 62 | + |
| 63 | +You will run this function whenever you want to do something which is on a cooldown. For example, |
| 64 | +if you have a special item which you only want the player to be able to use once every 5 seconds, |
| 65 | +you would run this function to check if the player is on cooldown or not. |
| 66 | + |
| 67 | +In this function, we check the executor's `my_cd` score to tell whether they have an active cooldown. |
| 68 | +- If the score is 1 or higher (i.e. they haven't reached the end of the cooldown), then the function will return a **failure**.. |
| 69 | +- If the score is 0 or doesn't exist yet (i.e. they do not have an active cooldown), then the function will return a **success**. |
| 70 | + |
| 71 | +```mcfunction:check_cooldown.mcfunction |
| 72 | +# If the player is still on cooldown, tell the player and stop here. |
| 73 | +execute if score @s my_cd matches 1.. run tellraw @s {color:"red",text:"This is on cooldown! Wait before you try again."} |
| 74 | +execute if score @s my_cd matches 1.. run return fail |
| 75 | + |
| 76 | +# If the function hasn't yet been stopped, then the player is not on a cooldown |
| 77 | +# so we can set the cooldown and then return a success. |
| 78 | + |
| 79 | +scoreboard players set @s my_cd 100 |
| 80 | +return 1 |
| 81 | +``` |
| 82 | + |
| 83 | +## 4. Using the `check_cooldown` function |
| 84 | + |
| 85 | +Whenever the player does something that needs a cooldown, you can use `execute if function` with the `check_cooldown` function. If the |
| 86 | +player still is on a cooldown, then the function will fail and the command will not run. If the player isn't on a cooldown, then |
| 87 | +the function will set the cooldown and then return a success, so the command WILL run. |
| 88 | + |
| 89 | +In this example, a creeper will only be summoned when the player has no active cooldown (in which case, the cooldown will be set). |
| 90 | +If the player has an active cooldown, then the creeper will not be summoned, and instead the error message in `check_cooldown.mcfunction` |
| 91 | +will be sent. |
| 92 | + |
| 93 | +```mcfunction |
| 94 | +execute as <player> if function <namespace>:check_cooldown run summon creeper |
| 95 | +``` |
| 96 | + |
| 97 | +## Using multiple cooldowns |
| 98 | + |
| 99 | +If you want to have multiple different cooldowns for different items, then you will need a different `cooldown` scoreboard and `check_cooldown` |
| 100 | +function for each cooldown. You will also need to decrease the score for each cooldown in the tick function. |
0 commit comments