Tutorial by: CharcoalToast

0. Introduction.

If you are redirected to this tutorial by a member of one of the Skript communities, that will probably mean that your indentation was wrong. You probably did not know what they meant and you just wanted your error to be resolved, but this is actually a rather small, but really important aspect of coding in general, so that's why this tutorial was created.

So basically, if you experienced the error "Error: Indentation error at line X, expected X tabs/spaces, but found X tabs/spaces" or something similar, and you don't know how to fix it, read this tutorial.

1. What is indentation?

Indentation is placing text in code further to the right, to allow the parser to know this is a different section of the code. Most programming language don't care about indentation, their parser can handle it correctly without, but Skript can't, because of its rich, English-like syntax.
That's why indentation is really important and you should always take care of it. See an example below.

on script load:
    # Here I indentated with one tab, to clarify this is the section after the event.
    if "Hello" does not equal "Bye":
        # Here I indentated with two tabs, because this is a section after a condition, that already was indentated.
        send "How are you?" to console

2. Rules of indentation.

When should I indentate?

on script load: #This is an event, so the next line should be indentated once.

if attacker is a player: # Condition, so the next line should be indentated once.
else if type of current inventory of player is anvil inventory: # Also a condition.
else: # Believe it or not, this is a condition as well.

while player is online: # Something that requires a colon is a section and therefore needs indentation.
loop all players:

function myFunc(): # A function is declared at root-level.
command /test: # Same for commands.

How should I indent?

3. Examples.

Look at the examples below and see if you understand why indentation is needed.

on damage:
    # One indentation because of the event.
    if attacker is a player:
        # Two indentations because of the event and the condition.
        if victim is a player:
            # Three indentations because of the event and the two conditions.
            broadcast "%attacker% murdered %victim%"

    # One indentation because of the event
    else if attacker is a zombie:
        # Two indentations because of the event and the condition.
        loop all players:
            # Three indentations because of the event, the condition and the section (looping).
            give a diamond sword to loop-player

# Again root-level, because we are in a new event, so no indentation.
on rightclick with pickaxe: 
    # One indentation because of the event.
    add 1 to player's balance

Did you find CharcoalToast's tutorial helpful?

You must be logged in to comment

  • May 17, 2020, 4:07 p.m. - CharcoalToast  

    Please give feedback on how the tutorial was created. The content may seem to be not important, but I can guarantee you that this is one of the main struggles for starting Skript-coders.

    Please reference to this tutorial as well when you find someone who indents incorrectly.

    June 8, 2020, 5:14 p.m. - Knifu  


  • July 12, 2021, 4:18 p.m. - TheDerpyFreddy  

    Nice tutorial! Add the end of the example, how did you get the balance?

  • Oct. 22, 2021, 3:37 p.m. - EnchantedApple  

    Amazing tutorial. You helped me a lot