Tutorial by: steveshat


Among many new coders in the skript community, many people have questions about loops.

Some common questions about loops:

Here I will be answering all of those questions.

What are loops

To put it in simple terms, a loop will go through a list of data and repeat the code for each value. Loops are useful for gathering multiple units of data.

The second type of loops in skript is a while loop. While loops will run set code repeatedly until the condition returns otherwise.

What can I loop?

In skript you can loop a variety of things. You can loop players, blocks, lists, and many more things!

loop all players:
loop all blocks in radius 5 of player:
loop {list::*}:

How do I use loops?

Skript makes loops fun and easy to use.
First we start off by addressing what we want to loop.

loop all players:

Next we can address the player we have looped by using loop-player. Lets use this to make some fun code!

loop all players:
    send "Hey, I like your skin!" to loop-player

Now that we have some basic knowledge on loops, we can loop some other things.
Lets loop the blocks around a player and set them to diamond! First we start off by looping the blocks.

loop all blocks in radius 3 of player:

Now we can use our knowledge of looping to set the blocks to diamond.

loop all blocks in radius 3 of player:
    set loop-block to diamond block
    send "Woah... what just happened?" to player

Lastly, lets go over looping a list.

We are going to start off with some basic string data in our list.

on load:
    add "Hey, whats up?" to {coolList::*}
    add "Nothing much. You?" to {coolList::*}

Now that we have our list setup lets go ahead and loop it.

on load:
    add "Hey, whats up?" to {coolList::*}
    add "Nothing much. You?" to {coolList::*}

command /loop:
    trigger:
        loop {coolList::*}:

Okay cool, now we have successfully looped our list. The next question most people will ask is how do I address the data I have looped? We can address these strings we have added to our list by using loop-value. Lets go ahead and broadcast the strings we looped.

on load:
    add "Hey, whats up?" to {coolList::*}
    add "Nothing much. You?" to {coolList::*}

command /loop:
    trigger:
        loop {coolList::*}:
            broadcast "%loop-value%"

Awesome! You just made a really cool loop, congrats!

How to use While Loops

First lets start off with creating our while loop and making a condition for it.

while player is online:

One important thing to know about while loops is that you always should add a delay or your server will crash. Now that we have that setup lets add some code!

while player is online:
    send "Hey %player%, its nice to see you here"
    wait 30 seconds

Now we have successfully created a while loop. To break it down, this will send the message every 30 seconds to the player while they are online. The loop will stop once the player disconnects from the server.

If your interested in making code run infinitely while your script is loaded here is how.
We start off by adding a while loop on load.

on load:
    while true = true:

Cool, now we have a loop that will run infinitely while the script is loaded. Now lets add some fun code to it.

on load:
    while true = true:
        broadcast "This is an automatic broadcast"
        wait 1 minute

Now we have successfully created an auto broadcast script! Congrats, now always remember to add delays to your while loops!

Thats all I will be going over for now. Leave comments on what I should add or take out.


Did you find steveshat's tutorial helpful?


You must be logged in to comment

  • Aug. 9, 2022, 3:32 p.m. - GStudiosX  

    This comment has been removed.

  • Aug. 25, 2022, 8:21 p.m. - DjDisaster  

    Not very well explained.

    |