Loops in BDFD: $for & $loop
Loop (Function only available on Bot-Creator !)
In this guide
A loop allows your Discord bot to repeat a block of text or actions multiple times. Instead of copy-pasting the same lines over and over, you can use a loop to do it automatically.
In BDFD, the functions $for and $loop are identical twins. They work exactly the same way, support the same rules, and can be used completely interchangeably! Each loop must be closed by its own end tag: $for with $endfor, $loop with $endloop (using the other one is an “Unexpected $endfor/$endloop” error).
Loops are perfect for:
- Printing repeating lists (like leaderboard slots or store items).
- Generating multiple buttons or select options.
- Counting up or down in a message.
💡 Quick Overview of Loop Types
BDFD supports two main types of loops (there is also a list form, $for[item;a;b;c], and $while, see the end of this guide):
| Loop Type | What it does | Best For |
|---|---|---|
| Simple Loop | Repeats a set number of times | Repeating a message or list item a specific number of times. |
| Advanced Loop | Counts using custom starting points, steps, and rules | Counting down, skipping numbers (like counting even numbers), or tracking custom counters. |
1. Simple Loops
A Simple Loop runs your code a set number of times.
How to Write It
You can write a simple loop using either $for or $loop:
$for[number]
... your text or command here ...
$endfor
OR
$loop[number]
... your text or command here ...
$endloop
number: How many times you want the loop to repeat (e.g.,5, or a variable like$getUserVar[hunts]). It must be a non-negative whole number; anything else (-1,abc,2.5) is an error (“Expected a nonnegative integer loop count.”).
Useful Shortcut Variables
Inside the loop, you can use these shortcuts to show the current iteration number:
$i(or$loopIndex/$loopIteration): Returns the current step, starting at0(i.e.0,1,2,3…).$loopCount: Returns the current step, starting at1(i.e.1,2,3,4…).
Practical Examples
Example A: Repeating Text (Using $loop)
$loop[3]
Hello! This is message number $loopCount
$endloop
What your bot will send:
Hello! This is message number 1
Hello! This is message number 2
Hello! This is message number 3
Example B: Creating a Bulleted List (Using $for)
🏆 **Top Server Goals:**
$for[5]
• Goal #$loopCount: [Pending Slot]
$endfor
What your bot will send:
🏆 Top Server Goals:
• Goal #1: [Pending Slot]
• Goal #2: [Pending Slot]
• Goal #3: [Pending Slot]
• Goal #4: [Pending Slot]
• Goal #5: [Pending Slot]
2. Advanced Loops (C-Style)
Advanced loops give you full control. You can decide where the loop starts, when it should stop, and how it counts.
How to Write It
Just like simple loops, you can use either $for or $loop with the three counting settings separated by semicolons:
$for[start; condition; update]
... your text or command here ...
$endfor
OR
$loop[start; condition; update]
... your text or command here ...
$endloop
The Three Settings:
start: Create your counter variable and set its starting value.- Example:
i=1(creates a counter namedistarting at 1). - Example (Multi-counter):
i=1,j=10(creates two counters). - Counters are whole numbers.
- Example:
condition: Tells the loop when to keep going. The loop stops as soon as this is no longer true.- Operators you can use:
<(Less than)<=(Less than or equal to)>(Greater than)>=(Greater than or equal to)==(Equal to)!=(Not equal to)
- Both sides must be whole numbers (a single
=is an error: “Invalid integer loop condition”). - Example:
i<=5(keep going as long as the counteriis 5 or less).
- Operators you can use:
update: Tells the loop how to count at the end of each step.- Stepping values you can use:
i++(add 1 toi)i--(subtract 1 fromi)i+=2(add 2 toi- useful for skipping numbers)i-=3(subtract 3 fromi)i*=2(multiplyiby 2)i/=2(divideiby 2, dividing by zero is an error)
- Several updates are separated by commas, e.g.
i++,j--. An update can only change a counter declared instart.
- Stepping values you can use:
Accessing your Counter Value
To show the current value of your counter inside the loop, just write its name as a function:
- If you set
i=1, use$ito display its value. - If you set
count=10, use$countto display its value.
Practical Examples
Example A: Counting Even Numbers (Using $loop)
Counting by twos:
$loop[i=2; i<=10; i+=2]
- $i
$endloop
What your bot will send:
Counting by twos:
- 2
- 4
- 6
- 8
- 10
Example B: T-Minus Countdown (Using $for)
$for[i=5; i>0; i--]
🚀 Launching in $i...
$endfor
🔥 BLASTOFF! 🎆
What your bot will send:
🚀 Launching in 5...
🚀 Launching in 4...
🚀 Launching in 3...
🚀 Launching in 2...
🚀 Launching in 1...
🔥 BLASTOFF! 🎆
⚠️ Important Rules & Safety Tips
[!WARNING] Execution limit There is no fixed “100 repetitions” cap. Instead, the whole script has a step budget shared by every statement and loop iteration. A loop that runs too long (for example
$while[1==1], or a huge count such as$for[1000000]) stops the command with the error “Execution step limit exceeded.” Keep your loops as small as possible.
[!TIP] Stopping Loops Early
$break: leaves the current loop.$continueskips to the next iteration (both are errors outside a loop).$stop: halts the whole command immediately and sends whatever text has been generated so far.$cooldown[...]: if the user is on cooldown, the command stops (including the loop) and the cooldown message is sent.
Other loop forms
- List loop:
$for[item;a;b;c]runs once per listed value;$itemis the current value (the first argument must be a plain name). - While loop:
$while[condition]…$endwhilerepeats as long as the condition is true;$iis the 0-based iteration.