What is Delay Cooldown?

Delay Cooldown
The delay cooldown feature is a favorite among beginners scripters due to its ease of use. However, there's a common mistake when using it for long periods of time, such as waiting for 24 hours. If the server closes before the 24-hour period elapses, the expression will not work. Therefore, it's worthwhile to consider when and how to use the delay cooldown effectively. Therefore, it's wise to combine delay cooldown with another feature to ensure the expression is executed within the desired timeframe.

When to Employ a Daily Cooldown?
Let's say you want to make the player to wait 5 seconds before teleporting to spawn
command /spawn:
    cooldown: 5 seconds
    cooldown message: Before reusing the command, wait %remaining time%.
    trigger:
        teleport player to spawn of world "world"


In order to add the daily, all you have to do is type and (wait|halt) [for] %timespan%. After the time has passed, the script will run the next expression, and for our example we do wait 5 seconds.
command /spawn:
    cooldown: 5 seconds
    cooldown message: Before reusing the command, wait %remaining time%.
    trigger:
        send "wait 5 seconds and you will be teleported to the spawn"
        wait 5 seconds
        teleport player to spawn of world "world"

Using Delay Cooldown with Loops
Delay cooldown is often combined with loops to create more dynamic effects. Here's an example using a loop:
command /countdown:
    trigger:
        #everything under this will be repeated 5 times
        loop reversed 5 times:
            send loop-value
            wait 1 second

This command sends a countdown message to the player, displaying values from 5 to 1, each with a 1-second delay. Loops enhance interactivity and allow for creative sequences.

Employing Delay Cooldown with While Loops
While loops provide another way to leverage delay cooldown. Here's an example
on join:
    while player is online:
        send action bar "Health %health of player%/%maximum health of player%" to player
        wait 1 tick

While the player is online, this code continuously updates the player's health in the action bar. However, it's essential to include a delay within the while loop to prevent server crashes.
Note: When using a while loop, be cautious not to forget to include a delay. Failure to do so may result in server crashes.

Combining Delay Cooldown with Variables
By incorporating loops and variables, you can create more intricate effects. Consider this example where the player must remain stationary for 5 seconds to trigger a teleport:
command /spawn:
    cooldown: 5 seconds
    cooldown message: Before reusing the command, %remaining time%.
    trigger:
        send "Don't move for 5 seconds to initiate teleportation"
        set {_location} to player's location
        loop 5 times:
            if distance between the player and {_location} <= 1:
                send loop-value
                wait 1 second
            else:
                send "Teleportation canceled"
                stop trigger
        teleport player to spawn of world "world"

In this command, a player's movement is monitored for 5 seconds. If they remain still, the teleportation occurs; otherwise, it's canceled. This showcases the power of combining delay cooldown with loops and variables to create sophisticated in-game interactions.