How to make this not lag? (leaderboard)

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

Dec 3, 2024
10
2
3
Whenever someone uses this it lags the server really bad because i loop all the offline players and i have a pretty large server. Anyway to work around this?

Code:
command /playtimeTop:
    trigger:   
        loop all offline players:
            set {time::%loop-offline player's uuid%} to time played of loop-offline player
        send "&8----------------- &eTop Playtime&8-----------------"
        set {_top::*} to sorted indices of {time::*} in descending order
        loop 10 times:
            add {_top::%loop-value%} to {_top_10::*}
        loop {_top_10::*}:
            add 1 to {_n}
            send "&a&l%{_n}% &7- &2%{_top_10::%{_n}%} parsed as offlineplayer% &7- &6%{time::%{_top_10::%{_n}%}%}% " to player
        set {_n} to size of {_low.to.high.list::*}
        loop {_low.to.high.list::*}:
            set {_high.to.low.list::%{_n}%} to loop-value
            set {_n} to {_n}-1
        loop {_high.to.low.list::*}:
            add 1 to {_topnumber}
            stop loop if {_topnumber} = 11
            set {_player} to "%loop-value%" parsed as offline player
            send "&9##%{_topnumber}% &8&l» &f%{_player}% &8- &e%{time::%loop-value%}%"
        send "&8----------------- &eTop Playtime&8-----------------"
 
I don't know how you ended up with 5 loops for a leaderboard script but you should only need 1
some other tips I can give is to move the sorting system to a periodical event so its sorted once in a while and cached
heres how the final code should look like
Code:
on leave
  save played time in var

every 5 minutes
  loop online players
    save played time in var

  set {_top-uuids::*} to sorted indices of {playtime::*}
  loop 10 times:
    set {_p} to {_top-uuids::%loop-value%} parsed as offline player
    set {_t} to {playtime::%{_top-uuids::%loop-value%}%}
    set {playtime.top::%loop-value%} to "%loop-number%. %{_p}% - %{_t}%"

command playtimetop
  send {playtime.top::*} to player
if it still lags you can try moving the sorting part to another thread
 
I personally would go with

Code:
every 10 seconds:
    loop online players:
        save played time in var
        wait 5 ticks

    set {_top-uuids::*} to sorted indices of {playtime::*}
      loop 10 times:
        set {_p} to {_top-uuids::%loop-value%} parsed as offline player
        set {_t} to {playtime::%{_top-uuids::%loop-value%}%}
        set {playtime.top::%loop-value%} to "%loop-number%. %{_p}% - %{_t}%"
        wait 1 tick
I have something like it on my server and I don't get any lag.[/CODE]
 
I converted it into a leaderboard, would you guys also know how to make it so it only shows like the top 2 and not all the seconds/minutes

1746302983792.png
 
If you want only top 2, just loop 2 times ig..
For the seconds thing - just split the time and display only what you want
 
the way i have it setup rn, it shows the time:: variable which is set to this
Code:
loop all offline players:
            set {time::%loop-offline player's uuid%} to time played of loop-offline player
and it automatically puts all the seconds and stuff
 
ye well then split the variable in somewhere where you want and display the other half