How to make a loop stop when a block is broken?

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

qAxl

Member
Apr 30, 2020
1
0
0
I am very new to skript and I am wondering how to make a loop stop when the event-block is broken.

Code:
on place:
    if event-block is hay bale:
        send "&6&lWheat Generator has been placed!" to player
        loop all players:
            loop all blocks in radius 10 around loop-player:
                if loop-block is hay bale:
                    loop 9999999 times:
                        wait 5 seconds
                        drop a wheat named "&a&lWheat" at loop-block's location

on break:
    if event-block is hay bale:
        send "&6&lWheat Generator has been broken!" to player
 
Not sure but:
Code:
on place:
    if event-block is hay bale:
        send "&6&lWheat Generator has been placed!" to player
        loop all players:
            loop all blocks in radius 10 around loop-player:
                if loop-block is hay bale:
                    while loop-block is hay bale:
                        wait 5 seconds
                        drop a wheat named "&a&lWheat" at loop-block's location
[doublepost=1588210247,1588210081][/doublepost]I have an easier solution for you by the way:
Code:
on place:
    if event-block is hay bale:
        while event-block is hay bale:
            wait 5 seconds
            drop a wheat named "&a&lWheat" at event-block's location
 
I am very new to skript and I am wondering how to make a loop stop when the event-block is broken.

Code:
on place:
    if event-block is hay bale:
        send "&6&lWheat Generator has been placed!" to player
        loop all players:
            loop all blocks in radius 10 around loop-player:
                if loop-block is hay bale:
                    loop 9999999 times:
                        wait 5 seconds
                        drop a wheat named "&a&lWheat" at loop-block's location

on break:
    if event-block is hay bale:
        send "&6&lWheat Generator has been broken!" to player
there are two types of loops, the for loop, ex:
Code:
loop {list::*}:
    #code
and the while loop, ex:
Code:
while player is riding:
    #code
    wait 5 ticks
we usually use the for loop when we know when will the repetition ends, unlike the while loop is mostly used when we don't know when will we break the loop. Here is an example:

Code:
while player is online:
    #code
    wait 10 ticks
I made a while loop that execute repetitively while the player is online, we don't know when will the player leave the server, do we ?

so we will check if the placed block is a hay bale, while the block at the event location is still a hay bale we execute the code
the finnal script will look like this:
Code:
on place:
    set {_location} to event-location
    if type of event-block is hay bale:
        set {wheat-generators::%{_location}%} to {_location} #just in case you want to save the location of that generator
        send "&6&lWheat Generator has been placed!" to player
        while block at {_location} is a hay bale:
            wait 5 seconds
            drop a wheat named "&a&lWheat" at {_location}
        delete {wheat-generators::%{_location}%}
 
Last edited:
Status
Not open for further replies.