proracer Posted February 27, 2011 Share Posted February 27, 2011 I was trying to create top 5 cash command but I don't know how to return top 5 players cash from element data. For example this function just returns player id and their amount of money. function top5Cash ( ) for i, player in ipairs ( getElementsByType ( "player" ) ) do local getPlayerCash = getElementData ( player, "data.cash" ) outputChatBox ( i ) outputChatBox ( getPlayerCash ) end end addCommandHandler ( "topcash", top5Cash ) So ... how can I return top 5 players with their cash from this element data? Link to comment
12p Posted February 28, 2011 Share Posted February 28, 2011 There is an error in that script. There are 2 types of value here: STRING and NUMBER. OutputChatBox SAYS IN THE DOCUMENTATION that requires a STRING. And you are giving it, the 2 times, invalid arguments, NUMBERS. Use: tonumber When you want to make numbers a string. Now the answer: function top5Cash ( ) top5 = { } mytable = { } for i, pl in ipairs ( getElementsByType ( "player" ) ) do local cash = getElementData ( pl, "data.cash" ) table.insert ( mytable, cash ) end --This sorts the table from max money values to minor. table.sort ( mytable,function ( a, b ) return a>b end ) --For the first 5 values encountered at the table, insert it into the top 5 table for i = 1, 5 do table.insert ( top5, mytable[5] ) end return top5 end addCommandHandler ( "topcash", top5Cash ) That will only return the table, huh. Now the other part, do it by yourself. Link to comment
proracer Posted February 28, 2011 Author Share Posted February 28, 2011 Thanks I will test. Btw, I tested that script with tonumber and without and it's still outputting same information... Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now