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 Voting system

Discussion in 'Skript' started by Aralwen, Aug 22, 2018.

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

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    Hello, I am trying to make a voting system. The vote is open for 2 minutes, the result is announced after this time. The player who has been voted the most receives a private message.

    I do not know how detect the person who has received the most votes.
    I would also like to know how to detect an equality


    My old code (look down for the new)


    Code (Skript):
    1. command /openvote:
    2.     description: Open the votes
    3.     trigger:
    4.         set {vote} to now
    5.         clear {votep::*}
    6.         broadcast "The votes are open for two minutes! /vote PSEUDO"
    7.         wait 2 minutes
    8.         broadcast "The most voted player is: ...."
    9.  
    10. command /vote [<player>]:
    11.     description: Vote a player
    12.     trigger:
    13.         if difference between {vote} and now is less than 2 minutes:
    14.             if {vote::%player%} is not set:
    15.                 set {votep::%player%} to now
    16.                 add 1 to {ListVote.%arg-player%}
    17.                 send "&aYour vote has been counted"
    18.             else:
    19.                 send "&cYou have already voted."
    20.         else:
    21.             send "&cThe votes are not open yet"
    Thanks,
    Aralwen.
    --- Double Post Merged, Aug 23, 2018, Original Post Date: Aug 23, 2018 ---
    Hello ! I modified my code a bit, but it still does not work and I do not understand why.
    So I'm summarizing what I would like my code to do :
    • A player starts the vote with /openvote, the players have 2 minutes to vote.
    • After 2 minutes, the most voted player is announced with the number of votes he has
    • In case of a tie, it sends a message of the style "The players have not managed to decide"
    • If no one has voted, it says so
    Thank's in advance for your help !

    New code:

    Code (Skript):
    1. command /openvote:
    2.     description: Open the votes
    3.     trigger:
    4.         set {VoteTimer} to now
    5.         clear {vote::*}
    6.         clear {VoteList::*}
    7.         broadcast "&aThe votes are open for two minutes! Use &a&l/vote PSEUDO"
    8.  
    9. every 1 second:
    10.     if difference between {VoteTimer} and now is more than 2 minutes:
    11.         clear {VoteTimer}
    12.         loop {VoteList::*}:
    13.             if loop-value is higher than 0:
    14.                 set {_vote} to loop-value
    15.                 set {_voteplayer} to loop-index parsed as player
    16.         if {_voteplayer} is set:
    17.             broadcast "&eThe most voted player is: &e&l%{_voteplayer}% &e- Voted by &e&l%{VoteList::%{_voteplayer}%}% people"
    18.         else:
    19.             broadcast "&cNobody voted!"
    20.  
    21.  
    22. command /vote [<player>]:
    23.     description: Vote a player
    24.     trigger:
    25.         if difference between {VoteTimer} and now is less than 2 minutes:
    26.             if {Vote::%player's uuid%} is not set:
    27.                 set {Vote::%player's uuid%} to now
    28.                 add 1 to {VoteList::%arg-player%}
    29.                 send "&aYour vote has been counted"
    30.             else:
    31.                 send "&cYou have already voted."
    32.         else:
    33.             send "&cThe votes are not open yet"


     
    • Like Like x 1
  2. Best Answer:
    Post #4 by TPGamesNL, Aug 24, 2018
  3. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    You can use https://skriptlang.github.io/Skript/expressions.html#ExprSortedList to sort the list which contains the votes. After that you can get the last element from the list. After that you loop the original list, since the sorted expression doesn't keep indices (as far as I know). In the loop you check if the loop-value matches the last element of the sorted list, if it does, set a variable to the loop-index. Don't exit the loop here, because when you continue looping and the loop-value matches the last element again, it's a tie. You can check this by checking if the variable for the winner is set. If it is, it's a tie. After that you can use broadcast or whatevet you want to announce the winner
     
    • Like Like x 1
  4. Aralwen

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    Hello ! First of all thank you, thanks to your help I managed to do for the most voted player.
    The problem is that other people are also announced

    By cons I admit not knowing how to detect a tie :/

    Code (Skript):
    1. command /openvote:
    2.   description: Open the votes
    3.   trigger:
    4.     set {VoteTimer} to now
    5.     clear {vote::*}
    6.     clear {VoteList::*}
    7.     broadcast "&aThe votes are open for two minutes! Use &a&l/vote PSEUDO"
    8.  
    9. every 1 second:
    10.   if difference between {VoteTimer} and now is more than 1 minutes:
    11.     clear {VoteTimer}
    12.     loop {VoteList::*}:
    13.       if loop-value is higher than 0:
    14.         set {_sorted::*} to {VoteList::*} sorted from highest to lowest with output "@index &ewith &e&[email protected] &evotes !"
    15.         broadcast "&eThe most voted player is: &e&l%{_sorted::*}%"
    16.       else:
    17.         broadcast "&cNobody voted!"
    18.  
    19.  
    20. command /vote [<player>]:
    21.   trigger:
    22.     if difference between {VoteTimer} and now is less than 2 minutes:
    23.       if {Vote::%player's uuid%} is not set:
    24.         #set {Vote::%player's uuid%} to now
    25.         add 1 to {VoteList::%arg-player%}
    26.         send "&aYour vote has been counted"
    27.       else:
    28.         send "&cYou have already voted."
    29.     else:
    30.       send "&cThe votes are not open yet"
    31.  
     
    #3 Aralwen, Aug 24, 2018
    Last edited: Aug 24, 2018
    • Like Like x 1
  5. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    Try this:
    Code (Skript):
    1. every 1 second:
    2.     if difference between {VoteTimer} and now is more than 1 minutes:
    3.         clear {VoteTimer}
    4.         set {_sorted::*} to sorted {VoteList::*}
    5.         set {_top} to last element out of {_sorted::*}
    6.         if {_top} is not set:
    7.             broadcast "Noone voted!"
    8.             stop
    9.         loop {VoteList::*}:
    10.             loop-value is {_top}
    11.             if {_p} is set:
    12.                 broadcast "Tie!"
    13.                 stop
    14.             set {_p} to loop-index
    15.         broadcast "Winner: %{_p}% with %{_top}% votes"
     
    • Like Like x 1
    • Agree Agree x 1
  6. Aralwen

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    Thank you very much for your help, it works perfectly!
    I have one last question: How to define life to the most voted person?

    I tried :

    Code (Skript):
    1.         broadcast "Winner: %{_p}% with %{_top}% votes"
    2.         set {ActualVote} to {_p}
    3.         set {ActualVote}'s max health to 5
    But it does not work :/
     
    • Like Like x 1
  7. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    You can use https://skriptlang.github.io/Skript/expressions.html#ExprHealth
     
    • Like Like x 1
  8. Aralwen

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    Yes I know, but although the variable is on the name of the player it does not work.

    This may be related to the fact that the vote result gives the nickname without the uppercase in front of the nickname
    (Aralwen -> aralwen)

    Code (Skript):
    1.         [...]
    2.         broadcast "Winner: %{_p}% with %{_top}% votes"
    3.         set {ActualVote} to {_p}
    4.         wait 1 second
    5.         set {ActualVote}'s max health to 5 #Does not work
    6.         set the maximum health of {ActualVote} to 5 #Does not work
    7.         set {_health} to {ActualVote} health
    8.         damage {ActualVote} by %{_health} /2% hearts #Does not work
     
    #7 Aralwen, Aug 24, 2018
    Last edited: Aug 24, 2018
    • Like Like x 1
  9. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    Oh wait ofcourse (index of a list is always a text). Use
    Code (Skript):
    1. set {ActualVote} to {_p} parsed as a offline player
    --- Double Post Merged, Aug 24, 2018, Original Post Date: Aug 24, 2018 ---
    Oh and you should broadcast with the variable I just mentioned so it has the right capital letters
     
    • Like Like x 1
  10. Aralwen

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    Thank you, I fixed the problem for capitalization. However, the rest still does not work :/

    Code (Skript):
    1. every 1 second:
    2.     if difference between {VoteTimer} and now is more than 1 minutes:
    3.         clear {VoteTimer}
    4.         set {_sorted::*} to sorted {VoteList::*}
    5.         set {_top} to last element out of {_sorted::*}
    6.         if {_top} is not set:
    7.             broadcast "Noone voted!"
    8.             stop
    9.         loop {VoteList::*}:
    10.             loop-value is {_top}
    11.             if {_p} is set:
    12.                 broadcast "Tie!"
    13.                 stop
    14.             set {_p} to loop-index parsed as a offline player
    15.         broadcast "Winner: %{_p}% with %{_top}% votes"
    16.         set {ActualVote} to {_p} parsed as a offline player #Does not work
    17.         wait 1 second
    18.         set {ActualVote}'s max health to 5 #Does not work
    19.         damage {ActualVote} by %{_health} /2% hearts #Does not work
    --- Double Post Merged, Aug 24, 2018 ---
    It's my fault, it works. However, this condition still does not work : damage {ActualVote} by %{_health} /2% hearts

    Code (Skript):
    1. every 1 second:
    2.     if difference between {VoteTimer} and now is more than 1 minutes:
    3.         clear {VoteTimer}
    4.         set {_sorted::*} to sorted {VoteList::*}
    5.         set {_top} to last element out of {_sorted::*}
    6.         if {_top} is not set:
    7.             broadcast "Noone voted!"
    8.             stop
    9.         loop {VoteList::*}:
    10.             loop-value is {_top}
    11.             if {_p} is set:
    12.                 broadcast "Tie!"
    13.                 stop
    14.             set {_p} to loop-index parsed as a offline player
    15.         broadcast "Winner: %{_p}% with %{_top}% votes"
    16.         set {ActualVote} to {_p}
    17.         wait 1 second
    18.         set {ActualVote}'s max health to 5
    19.         damage {ActualVote} by %{_health} /2% hearts #Does not work
    20.         broadcast "%{ActualVote}%"
     
    #9 Aralwen, Aug 24, 2018
    Last edited: Aug 24, 2018
    • Like Like x 1
  11. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    Use
    Code (Skript):
    1. damage {ActualVote} by {_health} / 2 hearts
    but the {_health} variable isn't set anywhere so you will have to add that
     
    • Like Like x 1
  12. Aralwen

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    Actually, I realized after ! However, it still does not work :/
    damage {ActualVote} by {_health} / 2 hearts #Does not work

    Code (Skript):
    1. every 1 second:
    2.     if difference between {VoteTimer} and now is more than 1 minutes:
    3.         clear {VoteTimer}
    4.         set {_sorted::*} to sorted {VoteList::*}
    5.         set {_top} to last element out of {_sorted::*}
    6.         if {_top} is not set:
    7.             broadcast "Noone voted!"
    8.             stop
    9.         loop {VoteList::*}:
    10.             loop-value is {_top}
    11.             if {_p} is set:
    12.                 broadcast "Tie!"
    13.                 stop
    14.             set {_p} to loop-index parsed as a offline player
    15.         broadcast "Winner: %{_p}% with %{_top}% votes"
    16.         set {ActualVote} to {_p}
    17.         wait 1 second
    18.         set {ActualVote}'s max health to 5
    19.         wait 1 second
    20.         set {_health} to {ActualVote}'s health
    21.         damage {ActualVote} by {_health} / 2 hearts #Does not work
     
    #11 Aralwen, Aug 24, 2018
    Last edited: Aug 24, 2018
    • Like Like x 1
  13. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    Try setting the health variable to the player's health / 2 and remove the / 2 part from the damage effect
     
    • Like Like x 1
  14. Aralwen

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    It still does not work :c

    Code (Skript):
    1.         set {_health} to {ActualVote}'s health / 2
    2.         damage {ActualVote} by {_health} hearts
     
    • Like Like x 1
  15. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    Try broadcast the {_health} variable to test if that part works
     
    • Like Like x 1
  16. Aralwen

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    I do not understand, because if I make an command and I set it to player it works

    work :
    Code (Skript):
    1. damage player by {_health} hearts
    doesn't work :
    Code (Skript):
    1. damage {ActualVote} by {_health} hearts
     
    • Like Like x 1
  17. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    Add
    Code (Skript):
    1. broadcast "health: %{_health}%"
    to the code
     
    • Like Like x 1
  18. Aralwen

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    Code (Skript):
    1. command /debug:
    2.     trigger:
    3.         set {ActualVote} to player
    4.         set {ActualVote}'s max health to 10
    5.         set {_health} to {ActualVote}'s health / 2
    6.         damage {ActualVote} by {_health} hearts
    7.         send "&aDebug: %{ActualVote}% - %{_health}%"
    [​IMG]
     
    • Like Like x 1
  19. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    Try testing if the damage effect works
     
    • Like Like x 1
  20. Aralwen

    Aralwen Active Member

    Joined:
    May 26, 2017
    Messages:
    164
    Likes Received:
    14
    Yes, as I told you:

    work :
    Code (Skript):
    1. damage player by {_health} hearts
    doesn't work :
    Code (Skript):
    1. damage {ActualVote} by {_health} hearts
     
    • Like Like x 1
  21. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    107
    Medals:
    Try setting a local variable to the player variable as a string, parsing it as a player and than use the damage effect on that
     
    • Like Like x 1
Thread Status:
Not open for further replies.

Share This Page

Loading...