Denizen Script Commands


Commands are always written with a '-' before them, and are the core component of any script, the primary way to cause things to happen.
Learn about how commands work in The Beginner's Guide.


Showing 1 out of 184 commands...
NameForeach
Related Guide Pagehttps://guide.denizenscript.com/guides/basics/loops.html
Syntaxforeach [stop/next/<object>|...] (as:<name>) (key:<name>) [<commands>]
Short DescriptionLoops through a ListTag, running a set of commands for each item.
Full DescriptionLoops through a ListTag of any type. For each item in the ListTag, the specified commands will be ran for that list entry.

Alternately, specify a map tag to loop over the set of key/value pairs in the map, where the key will be <[key]> and the value will be <[value]>.
Specify "key:<name>" to set the key definition name (if unset, will be "key").

Specify "as:<name>" to set the value definition name (if unset, will be "value").
Use "as:__player" to change the queue's player link, or "as:__npc" to change the queue's NPC link.
Note that a changed player/NPC link persists after the end of the loop.

To end a foreach loop, do - foreach stop

To jump immediately to the next entry in the loop, do - foreach next

Note that many commands and tags in Denizen support inputting a list directly, making foreach redundant for many simpler cases.

Note that if you delay the queue (such as with Command:wait or Language:~waitable) inside a foreach loop,
the loop can't process the next entry until the delay is over.
This can lead to very long waits if you have a long list and a wait directly in the loop, as the total delay is effectively multiplied by the number of iterations.
Use Command:run if you want to run logic simultaneously for many entries in a list in a way that allows them to separately wait without delaying each other.
Related Tags<[value]> to get the current item in the loop
<[loop_index]> to get the current loop iteration number
Usage Example
# Use to run commands 'for each entry' in a manually created list of objects/elements.
- foreach <[some_entity]>|<[some_npc]>|<[player]> as:entity:
    - announce "There's something at <[entity].location>!"
Usage Example
# Use to iterate through entries in any tag that returns a list.
- foreach <player.location.find_entities[zombie].within[50]> as:zombie:
    - narrate "There's a zombie <[zombie].location.distance[<player.location>].round> blocks away"
Usage Example
# Use to iterate through a list of players and run commands automatically linked to each player in that list.
- foreach <server.online_players> as:__player:
    - narrate "Thanks for coming to our server, <player.name>! Here's a bonus $50.00!"
    - money give quantity:50
Groupqueue
Sourcehttps://github.com/DenizenScript/Denizen-Core/blob/master/src/main/java/com/denizenscript/denizencore/scripts/commands/queue/ForeachCommand.java#L31