1. 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 comminuty!

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

  2. LOOKING FOR A VERSION OF SKRIPT?

    You can always check out our Wiki for downloads and any other information about Skript!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Prevent duplicate "while player is online:" loops

Discussion in 'Skript' started by MultiverShaun, Sep 20, 2022.

Thread Status:
Not open for further replies.
  1. MultiverShaun

    MultiverShaun Member

    Joined:
    Aug 8, 2022
    Messages:
    27
    Likes Received:
    3
    Some time ago, I learned that
    Code (Text):
    1. on join:
    2.     while player is online:
    3.         #do stuff
    4.         wait 5 seconds
    is better than

    Code (Text):
    1. every 5 seconds:
    2.     loop all players:
    3.         #do stuff for that player
    in terms of performance, which I do see. However, I noticed that if you logged out and logged on to the server fast enough, you could make the loop run multiple times.

    Example:
    Code (Text):
    1. on join:
    2.     while player is online:
    3.         send "This loop was run" to player
    4.         #Player leaves and rejoins during this 5 second time period
    5.         wait 5 seconds
    This would make a new loop start for the player, while the old one, after waiting 5 seconds, sees that the player is in fact still online, and continues.
    Is there a way to make sure that this won't happen? Thanks so much!
     
  2. lotzy

    lotzy Active Member

    Joined:
    Mar 15, 2022
    Messages:
    179
    Likes Received:
    26
    Code (Text):
    1. on join:
    2.   if {-loops::%uuid of player%} is true:
    3.     stop
    4.   set {-loops::%uuid of player%} to true
    5.   while {-loops::%uuid of player%} is true:
    6.     # some code
    7.     wait 5 seconds
    8.     if player is not online:
    9.       delete {-loops::%uuid of player%}
    10.  
     
  3. MultiverShaun

    MultiverShaun Member

    Joined:
    Aug 8, 2022
    Messages:
    27
    Likes Received:
    3
    Thanks!
     
  4. lotzy

    lotzy Active Member

    Joined:
    Mar 15, 2022
    Messages:
    179
    Likes Received:
    26
    mark as solved.
     
Thread Status:
Not open for further replies.

Share This Page

Loading...