GTX Posted November 17, 2011 Share Posted November 17, 2011 Hello, I'm wondering how to get all offline players and get them into a top list (I have no idea where to start) Code: function tables() local table1 = {} local table2 = {} local players = getElementsByType("player") local data1 local data2 local data3 for i, v in ipairs(players) do local account = getPlayerAccount(v) if not (isGuestAccount(account)) then local dataValue = tonumber(getAccountData(account, "Points")) local name = getPlayerName(v) table.insert(table1, dataValue) table2[dataValue] = name end end table.sort(table1) if (#table1 == 1) then data1 = "1. " .. table2[table1[1]] .. " - " .. table1[1] .. " points." elseif (#table1 == 2) then data1 = "1. " .. table2[table1[2]] .. " - " .. table1[2] .. " points." data2 = "2. " .. table2[table1[1]] .. " - " .. table1[1] .. " points." elseif (#table1 >= 3) then data1 = "1. " .. table2[table1[#table1]] .. " - " .. table1[#table1] .. " points." data2 = "2. " .. table2[table1[#table1 - 1]] .. " - " .. table1[#table1 - 1] .. " points." data3 = "3. " .. table2[table1[#table1 - 2]] .. " - " .. table1[#table1 - 2] .. " points." end setTimer(outputChatBox,50,1,"Top 3 points:", root, 255,255,255) if (data1) then setTimer(outputChatBox,60,1, data1, root, 255,255,255) end if (data2) then setTimer(outputChatBox,70,1, data2, root, 255,255,255) end if (data3) then setTimer(outputChatBox,80,1, data3, root, 255,255,255) end table.remove( table1 ) table.remove( table2 ) end addCommandHandler("top", tables) Thanks in advance. Link to comment
50p Posted November 17, 2011 Share Posted November 17, 2011 Use https://wiki.multitheftauto.com/wiki/GetAccounts instead. Link to comment
GTX Posted November 18, 2011 Author Share Posted November 18, 2011 But the example will get only number of all accounts... Link to comment
Castillo Posted November 18, 2011 Share Posted November 18, 2011 (edited) function tables() outputChatBox("Top 3 points:", getRootElement(), 255,255,255) for index, data in ipairs(sortAccounts()) do outputChatBox("#"..tostring(index)..": ".. tostring(data.account) .." - ".. tostring(data.points), getRootElement(), 255, 255, 255) if index == 3 then break end end end addCommandHandler("top", tables) function sortAccounts() local rowdata = {} for index, account in pairs(getAccounts()) do rowdata[index] = { account = getAccountName(account), points = getAccountData(account,"Points"), } end local comparator = function (a, b) return (tonumber(a.points) or 0) > (tonumber(b.points) or 0) end table.sort(rowdata, comparator) return rowdata end Seems to be working. Edited November 18, 2011 by Guest Link to comment
50p Posted November 18, 2011 Share Posted November 18, 2011 function tables() outputChatBox("Top 3 points:", getRootElement(), 255,255,255) for index, data in pairs(sortAccounts()) do outputChatBox("#"..tostring(index)..": ".. tostring(data.account) .." - ".. tostring(data.points), getRootElement(), 255, 255, 255) if index == 3 then break end end end addCommandHandler("top", tables) function sortAccounts() local rowdata = {} for index, account in pairs(getAccounts()) do rowdata[index] = { account = getAccountName(account), points = getAccountData(account,"Points"), } end local comparator = function (a, b) return (tonumber(a.points) or 0) > (tonumber(b.points) or 0) end table.sort(rowdata, comparator) return rowdata end Seems to be working. I'd use ipairs in the tables() function since you want to sorted table to be iterated in the index order. pairs doesn't always iterate table as you'd expect. Anyway, I'd also change the return value in sortAccounts() function to return just the top 3 results instead of returning the entire table. That will also save you some lines in the table() function (unnecessary if statement). Link to comment
GTX Posted November 19, 2011 Author Share Posted November 19, 2011 function tables() outputChatBox("Top 3 points:", getRootElement(), 255,255,255) for index, data in ipairs(sortAccounts()) do outputChatBox("#"..tostring(index)..": ".. tostring(data.account) .." - ".. tostring(data.points), getRootElement(), 255, 255, 255) if index == 3 then break end end end addCommandHandler("top", tables) function sortAccounts() local rowdata = {} for index, account in pairs(getAccounts()) do rowdata[index] = { account = getAccountName(account), points = getAccountData(account,"Points"), } end local comparator = function (a, b) return (tonumber(a.points) or 0) > (tonumber(b.points) or 0) end table.sort(rowdata, comparator) return rowdata end Seems to be working. Thank you! But this only gets account names, can it get player's names too? Link to comment
Castillo Posted November 19, 2011 Share Posted November 19, 2011 You can't get the player name if he's not online. 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