Chapter 3: Storing Information

It's always useful to store information to use it at a later time. Most programming languages call these variables. Let's learn about them and situations where they can prove to be extremely useful.

In This Chapter

This chapter is dedicated completely to variables. We're going to be learning about variables, the different types of variables, how to use them, and a few examples.

Part 1: What's a variable?

A variable is a method of storing information for using later. Such as a command for setting the location of the hub, and teleport newly joined players to the location that the player set earlier. Variables are extremely easy to use! And they are one of the most useful features in Skript. Variables act just like expressions, they return information, they can be set, and you can do everything with them that you can do with expressions. Such as using it in a string (A string means a string of characters, or what you call text in English.)

Variables can be of different types, one of them being global. A global variable means that it can be accessed anywhere in your code, and even in other scripts! They are the most simple type of variables, and to use them, it's extremely simply. Here is an example.

luckynumber.sk
command /setluckynumber:
    trigger:
        set {luckynumber} to random integer between 1 and 5
        send "&7The lucky number has been set to %{luckynumber}%."

command /whatsluckynumber:
    trigger:
        send "&7The lucky number is currently %{luckynumber}%!"

There are a lot of things happening in the code above. Let's see what's going on. First of all, we're creating a new command named /setluckynumber. You can read about creating a simple command in Chapter 1 of this tutorial. Next, in the trigger of the command, we're setting the variable. To set the variable, we use set {variablename} to (value). (Remember! To use variables, you always have to enclose them in squiggly brackets!) For value I'm using the random number expression which you can find here. The syntax is [a] random (integer|number) (from|between) %number% (to|and) %number% and it returns a random number based on the 2 numbers that we input. In my case, it returns a random number from one to five. After setting the variable, I'm using the send effect to send a message to the command sender, telling them the new value of the variable. If you've read the previous chapter, you'll know that we'll have to surround the variable with percent signs %. And that's it for the first command.

For the second command, all I'm doing is sending the value of the {luckynumber} variable to the command sender. Which will be the same value from first commands.

Part 2: Local Variables

So, as we learnt earlier, there are multiple types of variables. One of them being global variables, which you can access with {variablename}. But there is another type, local variables. In local variables, only code that is part of the code block can access the variable. If you were to use the same example from Part 1: What's a variable? with local variables, the second command would not work, because only the first command would know the value of the variable, the second command wouldn't. For example, if you have a join event which sets a local variable to the name of the joined player, and then send the local variable to yourself via a command, it would send <none> because the command wouldn't know the value of the local variable. Local variables get deleted as soon as the command, event, condition, or any other code block ends. So, if you can't store information to use somewhere else, what's the point of local variables? Are they completely useless? No. They are not. Local variables are used a lot to organize code, and generally if you're working with large values in Skript, you use local variables to shorten them just a word or two. It also makes your code look better, and more understandable. Here's an example of a local variable:

join_message.sk
on join:
    set {_message} to "&cWelcome, %player%."
    set join message to {_message}

It doesn't seem too useful, but they are used a lot in loops or situations where multiple effects are being ran with the same expression.

Part 3: List Variables

And now, for the most useful variable, List Variables. (That is just my opinion) List variables are used to hold more than 1 piece of information, such as the list of all admins in. And it makes variable organization extremely easy and simple, since you can: - Add values to a list - Remove values from a list - Set a specific value in a list - Delete a specific value from the list - Set the entire list - Delete the entire list

List variables look something like {variablename::index}. variablename is the name of the variable, and index is the part of the list you want to access, it's an identifier which tells Skript where the object belongs in the list. You can get all the values of a list variable at once by using * instead of the index. You can also loop list variables by using loop {variablename::*}: but you don't need to learn this for now.

This all may seem very confusing, but once you start using list variables, it's really not that hard. List variables don't follow a single rule for setting and adding values, so you'll just have to look at some examples in order to understand how to use them. The rest of this part is filled with different examples of list variables usages so you can get an idea of how, and where to use them.

Variable names work like texts. This means that if you want to use a player's name in a loop variable (index), instead of {names::player's name} you will have to use {names::%player's name%}. If you want to insert a variable with an expression inside of it, into another variable. You would set a local variable x to variable a, and insert local variable x into variable b. This makes your code much easier to read.

admins.sk
command /staff:
    trigger:
        add "John" to {_admins::*}
        add "Michael" to {_admins::*}
        add "Technoblade" to {_admins::*}
        send "&7The server's admins are: %{_admins::*}%"
        # The server's admins are: John, Michael and Technoblade
homes.sk
command /sethome:
    permission: homes.sethome
    permission message: &cYou don't have permission!
    trigger:
        set {home::%player's uuid%} to player's location

command /home:
    permission: homes.teleport
    permission message: &cYou don't have permission!
    trigger:
        teleport player to {home::%player's uuid%}
joins.sk
on join:
    add 1 to {joins::%player's uuid%}

command /howmanyjoins:
    trigger:
        set {_joins} to {joins::%player's uuid%}
        send "&7You have joined the server %{_joins}% times."

Conclusion

Okay, you just learnt how to use variables in Skript! Now, you can go on to read Chapter 4 of this tutorial, in which we learn about Command Arguments which are basically the stuff that we enter after the command name. For example, /mycommand argument1 argument2.

Last updated