Solved Running multiple loops at the same time

  • 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.

Fiopon

New Member
Dec 26, 2018
6
0
0
I'm making a skript that creates a gui-based slots machine. I want to be able to run the spinning animation of the 3 "reels" I have at the same time, how can I do this? Here's my code so far:

(Code for win conditions and stuff not implemented yet)

Code:
command /playslots:
    permission: slots.play
    permission message: {@noperm}
    trigger:
        if player's balance < 2000:
            message "{@intro}Insufficient funds! You need %2000-player's balance% more."
            stop
        set {_reelitems::*} to yaml list "Reel.Items" from file "plugins/Skript/SaveFiles/Slots/Reel.yml"
        set {_reelmax} to yaml value "Reel.Max" from file "plugins/Skript/SaveFiles/Slots/Reel.yml"
        set {_reel1} to random integer between 1 and {_reelmax}
        set {_reel2} to random integer between 1 and {_reelmax}
        set {_reel3} to random integer between 1 and {_reelmax}
        set {_reel1speed} to random integer between 1 and 4
        set {_reel2speed} to random integer between 1 and 4
        set {_reel3speed} to random integer between 1 and 4
        set {_defaultwin} to random integer between 2 and 8
        if player hasn't enough space for 1 stone sword:
            message "{@intro}Cannot play with a full inventory!"
            stop
        remove 2000 from player's balance
        set {slotsclosed.%player%} to false
        set {slotsfinished.%player%} to false
        open chest with 3 rows named "&3&lSlots Machine" to player
        wait 1 tick
#Blank Spaces
        set slot 0 of player's current inventory to white stained glass pane named "&r"
        set slot 1 of player's current inventory to white stained glass pane named "&r"
        set slot 3 of player's current inventory to white stained glass pane named "&r"
        set slot 5 of player's current inventory to white stained glass pane named "&r"
        set slot 7 of player's current inventory to white stained glass pane named "&r"
        set slot 8 of player's current inventory to white stained glass pane named "&r"
        set slot 9 of player's current inventory to lime stained glass pane named "&r"
        set slot 10 of player's current inventory to lime stained glass pane named "&r"
        set slot 12 of player's current inventory to lime stained glass pane named "&r"
        set slot 14 of player's current inventory to lime stained glass pane named "&r"
        set slot 16 of player's current inventory to lime stained glass pane named "&r"
        set slot 17 of player's current inventory to lime stained glass pane named "&r"
        set slot 18 of player's current inventory to white stained glass pane named "&r"
        set slot 19 of player's current inventory to white stained glass pane named "&r"
        set slot 21 of player's current inventory to white stained glass pane named "&r"
        set slot 23 of player's current inventory to white stained glass pane named "&r"
        set slot 25 of player's current inventory to white stained glass pane named "&r"
        set slot 26 of player's current inventory to white stained glass pane named "&r"
#Setup Reels
        loop 20 times:
            loop {_reel1speed} times:
                wait 2 ticks
                set {_reel1} to {_reel1}-1
                if {_reel1} < 1:
                    set {_reel1} to {_reelmax}
                if {_reel1} > {_reelmax}:
                    set {_reel1} to 1
                set {_reel1.1} to {_reel1}-1
                set {_reel1.2} to {_reel1}+1
                if {_reel1.1} < 1:
                    set {_reel1.1} to {_reelmax}
                if {_reel1.2} > {_reelmax}:
                    set {_reel1.2} to 1
                if {slotsclosed.%player%} is false:
                    set slot 2 of player's current inventory to {_reelitems::%{_reel1.1}%} parsed as item
                    set slot 11 of player's current inventory to {_reelitems::%{_reel1}%} parsed as item
                    set slot 20 of player's current inventory to {_reelitems::%{_reel1.2}%} parsed as item
            loop {_reel2speed} times:
                wait 2 ticks
                set {_reel2} to {_reel2}-1
                if {_reel2} < 1:
                    set {_reel2} to {_reelmax}
                if {_reel2} > {_reelmax}:
                    set {_reel2} to 1
                set {_reel2.1} to {_reel2}-1
                set {_reel2.2} to {_reel2}+1
                if {_reel2.1} < 1:
                    set {_reel2.1} to {_reelmax}
                if {_reel2.2} > {_reelmax}:
                    set {_reel2.2} to 1
                if {slotsclosed.%player%} is false:
                    set slot 4 of player's current inventory to {_reelitems::%{_reel2.1}%} parsed as item
                    set slot 13 of player's current inventory to {_reelitems::%{_reel2}%} parsed as item
                    set slot 22 of player's current inventory to {_reelitems::%{_reel2.2}%} parsed as item
            loop {_reel3speed} times:
                wait 2 ticks
                set {_reel3} to {_reel3}-1
                if {_reel3} < 1:
                    set {_reel3} to {_reelmax}
                if {_reel3} > {_reelmax}:
                    set {_reel3} to 1
                set {_reel3.1} to {_reel3}-1
                set {_reel3.2} to {_reel3}+1
                if {_reel3.1} < 1:
                    set {_reel3.1} to {_reelmax}
                if {_reel3.2} > {_reelmax}:
                    set {_reel3.2} to 1
                if {slotsclosed.%player%} is false:
                    set slot 6 of player's current inventory to {_reelitems::%{_reel3.1}%} parsed as item
                    set slot 15 of player's current inventory to {_reelitems::%{_reel3}%} parsed as item
                    set slot 24 of player's current inventory to {_reelitems::%{_reel3.2}%} parsed as item
            if {slotsclosed.%player%} is true:
                add 2000 to player's balance
                set {slotsclosed.%player%} to false
                stop
        set {slotsfinished.%player%} to true

on inventory close:
    if name of player's current inventory is "&3&lSlots Machine":
        if {slotsfinished.%player%} is true:
            set {slotsfinished.%player%} to false
            stop
        message "{@intro}Please don't close your inventory while playing slots! You have been refunded."
        set {slotsclosed.%player%} to true

on inventory click:
    if name of player's current inventory is "&3&lSlots Machine":
        cancel event

Edit:
A compromise that worked for me was nesting the loops in each other and speeding them up to the point where one might not notice they're not going at EXACTLY the same time.
 
Last edited:
Status
Not open for further replies.