Jump to content

need help with a small script


Recommended Posts

hi, I want to get the player that has the most cash, from the scoreboard. The problem is I don't know how to do it. Can someone explain or show me how to do it? please and thanks.

function maxMoney() 
local players = getElementsByType ( "player" ) 
    for theKey,thePlayer in ipairs(players) do 
        tableMoney = getElementData ( thePlayer, "money" ) 
        winnerMoney = math.max(unpack({tableMoney})) 
    end 
end 

Link to comment

Loop through players, check money (getElementData or actual money functions). Grab the first player you encounter, stuff into a variable. Then check every other player - replace the one in the variable if the current one has more money. At the end of the loop, you'll have the player with the most money.

Link to comment
Loop through players, check money (getElementData or actual money functions). Grab the first player you encounter, stuff into a variable. Then check every other player - replace the one in the variable if the current one has more money. At the end of the loop, you'll have the player with the most money.

I tried this, but it didn't work ( I get "nil" ) :

function maxMoney() 
local players = getElementsByType ( "player" ) 
    for theKey,thePlayer in ipairs(players) do 
        currentCash = getElementData ( thePlayer, "money" ) 
        if currentPlayer() and currentCash > storedCash then 
            currentPlayer(thePlayer) 
        end 
        winner = storedPlayer 
    end 
end 
  
function currentPlayer(source) 
    storedPlayer = source 
    storedCash = getElementData(storedPlayer, "money") 
end 

Link to comment
You could store all players and their money, and then use table.sort and return the first item from the table.

More than 2 people might have the same amount.

As for the code:

- unnecessary function currentPlayer, you're also calling it in a way that makes no sense

- don't use "source" as a variable name, just in case

- use local variables when you can

- use return values instead of assigning global variables

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...