Jump to content

One Question ...


proracer

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...