Chapter 4: Conditions

We don't always want to execute the code. Sometimes, we want that the code is only executed if a certain condition is met. So how do we do it? Let's find out.

In This Chapter

We're going to learn about conditions and different types of statements in Skript.

Part 1: Why Conditions?

Yes, I know. No one who is reading this tutorial asked this question. But it's very important to understand why conditions, and why NOT conditions. To start off, conditions are VERY useful in Skript. You know, I should probably stop saying that, I've said it in every chapter of this tutorial. Conditions are used a whole lot in Skript. For example, when a player joins, you can use a condition to confirm that their name does not contain a swear word, if it does, you can use the kick effect to warn them. This is just 1 of the many thousand examples I can think of. But there are situations in which a condition is not required. There are also a lot of times when new scripters make incorrect use of conditions, which leads to unappealing, unreadable, spaghetti code. You'll understand what I'm talking about later in this chapter.

Part 2: What Are Conditions?

In most programming languages, condition is a line of code that returns true or false. But Skript doesn't work like that. In Skript, a condition tells the code if the code should run or not. There are 2 types of conditions. An inline condition, and a block condition. In inline conditions, if the condition returns true, the code runs like normal. But if the condition returns false, the execution of code is immediately halted, and everything after the condition does not run. Block conditions end with a : semicolon, and work a bit differently. In block conditions, the code which you've already written works just fine. But the code you put in the next code block only runs if the condition returns true. Example of inline condition.

player's name is "Charlie"
kill the player
send "We don't like you, Charlie."
broadcast "Hello everyone".

In the above example, we have a condition which checks if the player's name is charlie, and if it is, it kills the player. But we also have a broadcast effect which broadcast "Hello everyone." to the server. The problem is, that even if the player's name isn't Charlie, our broadcast effect doesn't work, because the inline condition stops all code from executing if it returns false. We can use a block condition to fix this. Example of a block condition.

if player's name is "Charlie":
    kill the player
    send "We don't like you, Charlie."
broadcast "Hello everyone."

In this example, the text is broadcasted no matter if the player's name is Charlie or not.

Part 3: How To Use Conditions?

So now you know about block conditions and inline conditions. Now let's learn about statements. The one we used in the example above was an if statement. An if statement runs if the condition is met. An else statement comes after an if statement, and just like the name suggest, if the condition is met, then the code block runs, else the code in the else statement runs. There is also an else if statement. If the condition is met, run the code in the following code block. Else, if this other condition is met, then run the code in this code block instead. Else just run this other code. An example of this is:

on player join:
    if player's name is "Charlie":
        kill the player
        broadcast "Charlie died"
    else if player's name is "Landon":
        kill the player
        broadcast "Landon died"
    else:
        broadcast "%player% didn't die"

Conclusion

Now you know how to use conditions in Skript. You can either go read some code examples to learn more about conditions, or you can skip over to Chapter 5 to learn more in-depth about creating commands.

Last updated