Tutorial by: eult


Introduction

A Cooldown system is one of Skript's features that players often want to implement. By implementing a cooldown system, players may be prevented from spamming commands or using certain abilities too frequently. This can adversely affect the game's balance. The cooldown system can be used for certain commands or actions in the game

About Tutorial

In a skript, there are a variety of ways you can implement a cooldown, and as long as you are doing it in the right place, all of them are correct.
We will be going from the simplest to the most complex method in this tutorial.

Prerequisites

Before starting this tutorial, it is recommended that you have a basic understanding of variables and how to use them. If you need to brush up on these concepts, you can check out the Variables Tutorial here.

----------------------------------

- Command Cooldown

A command cooldown is the easiest and the most effective way to do a command cooldown and it's because skript developers support it.

When to use the Command Cooldown?

Let's say you want to add a cooldown to a simple command

command /cake:
    trigger:
        give a cake to the player

If you don't want the player to spam this command and gets how much cake they wont, so follow this tutorial how to create a cooldown!

Command cooldown is so super easy to add, just add cooldown effect before the trigger in the code

command /cake:
    cooldown: 1 hour
    trigger:
        give a cake to the player

That's it, now you have a cooldown.
If the user uses the command too early, you can send a cooldown message, making the cooldown more fancy

command /cake:
    cooldown: 1 hour
    cooldown message: wait %remaining time% to use this command again
    trigger:
        give a cake to the player

Here is an example of how you can play with it more and add more effects,

command /cake:
    cooldown: 1 hour
    cooldown storage: {cooldown::%player's uuid%::cake} #stores the cooldown in a variable to use in other events/commands
    cooldown message: wait %remaining time% to use this command again 
    cooldown bypass: cooldown.bypass #Give this permission to eliminate the cooldown for the specific player having this permission
    trigger:
        if the player has space for a cake:
            give a cake to the player
        else:
            send "inventory is full."
            cancel the cooldown #this will work like the cooldown bypass and well eliminate the cooldown 

Cooldown storage is useful because it allows you to cancel the cooldown using other commands

command /deleteCooldown:
    trigger:
        delete {cooldown::%player's uuid%::*}

The deleteCooldown command resets the cooldown from the previous command, so it's as if you had used the cancel the cooldown command already.

- Delay Cooldown

There is absolutely no doubt that the delay cooldown is one of the favorite features for every new skripter, and they make use of it a lot since it is so easy to use, but there is one problem, which is how they are using it, as I do not recommend anyone to use a delay cooldown when waiting for a long period of time since if a 24 hour wait expression is used and the server closes before the 24 hours run out, the expression will not work.

When to use the Delay 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"

The only thing you need to do is type wait 5 seconds and that's it.

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"

The delay is usually used with loops and while here is a Loop example:

command /countdown:
    trigger:
        loop 5 times: #everything under this will be repeated 5 times
            send loop-value
             #alternative, you can use 6 - loop-value [the 6 is how many times we did a loop + one] to count backwards 
            wait 1 second 

Here is a while example:

on join:
    while player is online:
        send action bar "Health %health of player%/%maximum health of player%" to player
        wait 1 tick

When you forget to use the delay Cooldown with the while condition, your server will crash!

Using loops and variables, you can make something super fancy

command /spawn:
    cooldown: 5 seconds #this is command cooldown
    cooldown message: Before reusing the command, %remaining time%.
    trigger:
        send "Don't move for 5 seconds and you will be teleported"
        set {_location} to player's location
        loop 5 times:
            if distance between the player and {_location} <= 1: #This will check if the player moved away from the used command location
                wait 1 second
                send loop-value
            else:
                send "Teleportation has been canceled"
                stop #this should stop the loop
        teleport player to spawn of world "world"

- Event Cooldown

The event cooldown can be tricky, because we are going to use the difference between expression, so before we begin, let's review what's the difference between expressions.

    difference (between|of) %object% and %object% #difference between syntax

As you can see, this expression will give you the difference between two objects of the same type, It's very useful expression I used it early on to check the blocks difference

When to use the Event Cooldown?

If you want to have a cooldown for any event

on walk on gold block:
   push the player upward at speed 0.6
   push the player horizontal forward at speed 3

In this case, we have to do it using a old method since skript does not support event like they do for the Command cooldown
let's start with the creating a variable that's we can use to check when the last time you used this event

    set {cooldown::%player's uuid%} to now

The now expression gives you the current system time of the server.
Once that is done, let's create a variable that compares now to {cooldown::%player's uuid%}. Using this variable, we can calculate the time difference.

    set {_waited} to difference between {cooldown::%player's uuid%} and now
    #You can also use the time since expression
    set {_waited} to time since {cooldown::%player's uuid%} 

Now let's create something to checks if the waited time is less than a the time we want so we can stop the player from using the event.

    if {_waited} is less than {@cooldown}:

Change the option-variable as you desire to increase/decrease the cooldown
Here's what we get when we add these all together:

on walk on gold block:
    set {_waited} to difference between {cooldown::%player's uuid%} and now
    if {_waited} is less than 10 seconds:
        send "Try again in %difference between 10 seconds and {_waited}%"
        stop
    else:
        push the player upward at speed 0.6
        push the player horizontal forward at speed 3
        set {cooldown::%player's uuid%} to now

This is the cooldown for this event, keep in mind that the {cooldown::%player's uuid%} variables will be saved in variables.csv, and it is not recommended to save this kind of short time use variables. So if you want to prevent it from being saved there, I recommend using RAM variables, which will be reset after the server has been shut down.

CHECK THE MEMORY-VARIABLES HERE

Discord: eult#0001


Did you find eult's tutorial helpful?


You must be logged in to comment