Solved Prevent duplicate "while player is online:" loops

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

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

MultiverShaun

Member
Aug 8, 2022
27
3
3
Some time ago, I learned that
Code:
on join:
    while player is online:
        #do stuff
        wait 5 seconds

is better than

Code:
every 5 seconds:
    loop all players:
        #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:
on join:
    while player is online:
        send "This loop was run" to player
        #Player leaves and rejoins during this 5 second time period
        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!
 
Some time ago, I learned that
Code:
on join:
    while player is online:
        #do stuff
        wait 5 seconds

is better than

Code:
every 5 seconds:
    loop all players:
        #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:
on join:
    while player is online:
        send "This loop was run" to player
        #Player leaves and rejoins during this 5 second time period
        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!
Code:
on join:
  if {-loops::%uuid of player%} is true:
    stop
  set {-loops::%uuid of player%} to true
  while {-loops::%uuid of player%} is true:
    # some code
    wait 5 seconds
    if player is not online:
      delete {-loops::%uuid of player%}
 
Code:
on join:
  if {-loops::%uuid of player%} is true:
    stop
  set {-loops::%uuid of player%} to true
  while {-loops::%uuid of player%} is true:
    # some code
    wait 5 seconds
    if player is not online:
      delete {-loops::%uuid of player%}
Thanks!
 
Status
Not open for further replies.