A Loop is a construct that repeats code for multiple values. A typical loop looks like this:
Code (Skript):
- loop <expression to loop>:
- <condition(s)>
- <effect(s)>
Simple loops can often be replaced with a single condition or effect by including the looped expression in it, e.g. use give a torch to all players instead of looping all players and giving them a torch individually.
Loopable Values:
All expressions that represent more than one value, e.g. 'all players', 'worlds', etc., as well as list Variables, can be looped. You can also use a list of expressions, e.g. loop the victim and the attacker, to execute the same code for only a few values.
List Variables:
When looping list Variables, you can also use loop-index in addition to loop-value inside the loop. loop-value is the value of the currently looped variable, and loop-index is the last part of the variable's name (the part where the list variable has its asterisk *). The following example will increase all values inside a list variable by one:
Code (Skript):
- loop {var::*}:
- set {var::%loop-index%} to loop-value + 1
While Loops:
Starting with version 2.0 you can use while loops, i.e. loops that will just keep repeating as long as a condition is met. A while loop looks like this:
Code (Skript):
- while <condition>:
- <code>
NOTE: While loops require at least a 1 tick wait in them, or they will crash your server, ex:
Code (Skript):
- while <condition>:
- <code>
- wait 1 tick