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 our 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 Points Leaderboard

Discussion in 'Skript' started by 07L, Nov 11, 2019.

  1. 07L

    07L Active Member

    Joined:
    Feb 23, 2019
    Messages:
    43
    Likes Received:
    0
    I have tried to think how I would make a leaderboard, but I cant get my head round how. I made a points system, it works fine and fully. But how would I make a leaderboard with the command /points leaderboard.
    Here's my current points file code (I haven't started the leaderboard yet as I couldn't figure out how and mess up the code).

    Code (Text):
    1. options:
    2.  
    3.     prefix: &8[&eAnimeme&8]
    4.  
    5. variables:
    6.     {points.%player%} = 0
    7.  
    8. command /points [<text>] [<offline player>] [<text>]:
    9.     trigger:
    10.         if player has permission "animeme.pointsadmin":
    11.             if arg-1 is set:
    12.                 if arg-1 is "give":
    13.                     if arg-2 is set:
    14.                         if arg-2 has played before:
    15.                             if arg-3 is set:
    16.                                 if (arg-3 parsed as num) is set:
    17.                                     set {_amount} to arg-3 parsed as a number
    18.                                     add {_amount} to {points.%arg-2%}
    19.                                     send message "{@prefix} &7Successfully added &c%arg-3% &7to &6%arg-2%&7's &7points!"
    20.                                 else:
    21.                                     send message "{@prefix} &cInvalid argument! You must specify a valid number to give."                              
    22.                             if arg-3 is not set:
    23.                                 send message "{@prefix} &cInvalid argument! Usage: &7/points give (player) (amount)"
    24.                         if arg-2 has not played before:
    25.                             send message "{@prefix} &cPlayer has not played before!"
    26.                     if arg-2 is not set:
    27.                         send message "{@prefix} &cInvalid argument! Usage: &7/points give (player) (amount)"
    28.                 else if arg-1 is "clear":
    29.                     if arg-2 is set:
    30.                         if arg-2 has played before:
    31.                             set {points.%arg-2%} to 0
    32.                             send message "{@prefix} &6%arg-2%&7's points have been cleared!"
    33.                         if arg-2 has not played before:
    34.                             send message "{@prefix} &cPlayer has not played before!"
    35.                     if arg-2 is not set:
    36.                         send message "{@prefix} &cInvalid argument! Usage: &7/points clear (player) "
    37.                 else if arg-1 is "remove":
    38.                     if arg-2 is set:
    39.                         if arg-2 has played before:
    40.                             if arg-3 is set:
    41.                                 if (arg-3 parsed as num) is set:
    42.                                     set {_amount} to arg-3 parsed as a number
    43.                                     if {_amount} is smaller than or equal to {points.%arg-2%}:
    44.                                         if {points.%arg-2%} is equal to 0:
    45.                                             send message "{@prefix} &cThis player has no points to be removed!"
    46.                                         else:
    47.                                             subtract {_amount} from {points.%arg-2%}
    48.                                             send message "{@prefix} &7Successfully took &c%arg-3% &7points from &6%arg-2%&7!"
    49.                                     else:
    50.                                         send message "{@prefix} &cThat number is bigger than the player's amount of points!"
    51.                                 else:
    52.                                     send message "{@prefix} &cInvalid argument! You must specify a valid number to remove."                              
    53.                             if arg-3 is not set:
    54.                                 send message "{@prefix} &cInvalid argument! Usage: &7/points give (player) (amount)"
    55.                         if arg-2 has not played before:
    56.                             send message "{@prefix} &cPlayer has not played before!"
    57.                     if arg-2 is not set:
    58.                         send message "{@prefix} &cInvalid argument! Usage: &7/points give (player) (amount)"
    59.                 else if arg-1 is "set":
    60.                     if arg-2 is set:
    61.                         if arg-2 has played before:
    62.                             if arg-3 is set:
    63.                                 if (arg-3 parsed as num) is set:
    64.                                     set {points.%arg-2%} to arg-3 parsed as a number
    65.                                     if {points.%arg-2%} is equal to 1:
    66.                                         send message "{@prefix} &7Successfully set &6%arg-2%&7's points to &a%arg-3% &apoint&7!"
    67.                                     else:
    68.                                         send message "{@prefix} &7Successfully set &6%arg-2%&7's points to &a%arg-3% &apoints&7!"
    69.                                 else:
    70.                                     send message "{@prefix} &cInvalid argument! You must specify a valid number to set."                              
    71.                             if arg-3 is not set:
    72.                                 send message "{@prefix} &cInvalid argument! Usage: &7/points give (player) (amount)"
    73.                         if arg-2 has not played before:
    74.                             send message "{@prefix} &cPlayer has not played before!"
    75.                     if arg-2 is not set:
    76.                         send message "{@prefix} &cInvalid argument! Usage: &7/points give (player) (amount)"
    77.                 else:
    78.                     send message "{@prefix} &cInvalid argument! Usage: &7/points (give/clear/remove/set) (player) (amount)"
    79.             if arg-1 is not set:
    80.                 send message "{@prefix} &7You have &a%{points.%player%}% &apoints&7."      
    81.         if player does not have permission "animeme.pointsadmin":
    82.             send message "{@prefix} &7You have &a%{points.%player%}% &apoints&7."
     
  2. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    195
    Likes Received:
    18
    Use list variables for player data so you can sort them into an order. Take a look at this tutorial I made on making a leaderboard:
    https://forums.skunity.com/threads/sorting-method-toplists.9931/
     
  3. 07L

    07L Active Member

    Joined:
    Feb 23, 2019
    Messages:
    43
    Likes Received:
    0
  4. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    195
    Likes Received:
    18
    Make sure you have skript-mirror.
    1. Do not store the player's points in a non list variable. Store it in a list, example: {points::%arg 2%}
    2. Use the expressions shown in the post:
    Code (Skript):
    1. import:
    2.     java.util.ArrayList
    3.     java.util.Collections
    4.     java.util.Map$Entry
    5.     ch.njol.skript.variables.Variables
    6.  
    7. expression replace values %strings% with %strings% in %string%:
    8.     get:
    9.         set {_string} to expression-3
    10.         loop expressions-1:
    11.             add 1 to {_index}
    12.             replace all "%loop-value%" with "%{_index}th element of expressions-2%" in {_string}
    13.         return {_string}
    14.  
    15. plural expression [the] (1¦(highest|top)|2¦(lowest|last)) %integer% values of %objects% [formatted] as %string%:
    16.     get:
    17.         set {_list} to new ArrayList(Variables.getVariable(raw expressions-2.getName().toString(event), event, raw expressions-2.isLocal()).entrySet())
    18.         {_list}.sort(Entry.comparingByValue())
    19.         if parse mark = 1:
    20.             Collections.reverse({_list})
    21.         loop ...{_list}:
    22.             "%loop-value.getValue()%" is not "0"
    23.             add 1 to {_index}
    24.             set {_sorted::%{_index}%} to replace values "@place", "@index" and "@value" with "%{_index}%", "%loop-value.getKey()%" and "%loop-value.getValue()%" in expression-3
    25.             {_index} = expression-1
    26.             stop loop
    27.         return {_sorted::*}
    28.  
    29. command /testsort:
    30.     trigger:
    31.         set {TopPoints::*} to the highest (size of {points::*}) values of {points::*} as "&e@place. &b@index &7- &6@value"
    3. {TopPoints::*} will contain all the players, their places, and the amount of points they have each in one string.

    Once again this is just an example. You can make it however you like.
     
  5. 07L

    07L Active Member

    Joined:
    Feb 23, 2019
    Messages:
    43
    Likes Received:
    0
    Thank you, it works almost perfectly. I have two questions though,
    1) Is it possible to make it display the players name with the capital letters in correct spaces?
    2) Is there a way to make it update when I clear the players points? (see image) upload_2019-11-12_19-43-54.png
     
  6. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    195
    Likes Received:
    18
    Yes there is. You can change this line of code in the expression for sorting:
    Code (Skript):
    1. set {_sorted::%{_index}%} to replace values "@place", "@index" and "@value" with "%{_index}%", "%loop-value.getKey()%" and "%loop-value.getValue()%" in expression-3
    Change it to this:
    Code (Skript):
    1. set {_sorted::%{_index}%} to replace values "@place", "@index" and "@value" with "%{_index}%", "%(loop-value.getKey()) parsed as offline player%" and "%loop-value.getValue()%" in expression-3
    If that does not work, let me know as it is untested.

    To update it, you have to do the:
    Code (Skript):
    1. set {TopPoints::*} to the highest (size of {points::*}) values of {points::*} as "&e@place. &b@index &7- &6@value"
    again. It is recommended to make an auto-updator or a manual way to update the list and not update it every time they use the command.
     
  7. 07L

    07L Active Member

    Joined:
    Feb 23, 2019
    Messages:
    43
    Likes Received:
    0
    How come it never removes the last player. I had 2 players, set their points to 0 yet it still displays one of them with their points even though they dont have any. Even with an updator to run every 3 seconds. However, the player names with capitalisation works, thanks.
     
  8. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    195
    Likes Received:
    18
    What code are you using to set someone's points? What you can also try is "delete {TopPoints::*}" before updating again.
     
    #8 AsuDev, Nov 12, 2019
    Last edited: Nov 12, 2019
  9. 07L

    07L Active Member

    Joined:
    Feb 23, 2019
    Messages:
    43
    Likes Received:
    0
    It's fine now, all works. Thank you so much!
     
    #9 07L, Nov 13, 2019
    Last edited: Nov 13, 2019
Loading...