TheMtaUser555 Posted July 10, 2012 Posted July 10, 2012 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
robhol Posted July 10, 2012 Posted July 10, 2012 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. Do NOT PM ME for help unless invited. - New MTA Script Editor Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.
TheMtaUser555 Posted July 10, 2012 Author Posted July 10, 2012 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
Castillo Posted July 10, 2012 Posted July 10, 2012 You could store all players and their money, and then use table.sort and return the first item from the table. San Andreas Utopia RPG (SAUR) Owner & Developer. Education is the most powerful weapon which you can use to change the world.
TheMtaUser555 Posted July 10, 2012 Author Posted July 10, 2012 You could store all players and their money, and then use table.sort and return the first item from the table. It worked, thanks!
robhol Posted July 10, 2012 Posted July 10, 2012 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 Do NOT PM ME for help unless invited. - New MTA Script Editor Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.
Castillo Posted July 10, 2012 Posted July 10, 2012 You could store all players and their money, and then use table.sort and return the first item from the table. It worked, thanks! You're welcome. San Andreas Utopia RPG (SAUR) Owner & Developer. Education is the most powerful weapon which you can use to change the world.
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