Snow-Man Posted November 23, 2022 Posted November 23, 2022 hello everyone i have a problem on my script that doesnt sort data when it gets new data into it function getKillStats() table.sort(topKillers, function (a, b) return a[1] < b[1] end ) return topKillers end how i add the new data by this way if topKillers[k] then topKillers[k] = topKillers[k] + 1 else topKillers[k] = 1 end USC Informations Head Staff & Owner Of USC. Youtube Channel : My Channel
roaddog Posted November 23, 2022 Posted November 23, 2022 then you have to call table.sort everytime you add new data You gone learn today. I work my ass off, but I still can't pay tho.
Snow-Man Posted November 24, 2022 Author Posted November 24, 2022 Already tried that but didn't work USC Informations Head Staff & Owner Of USC. Youtube Channel : My Channel
roaddog Posted November 24, 2022 Posted November 24, 2022 (edited) how your table looks like? tkill = {{"aa", 10}, {"bb", 2}, {"cc", 5}} -- Sort the table from high to low table.sort(tkill, function (a, b) return a[2] > b[2] end) for i, v in pairs(tkill) do print(v[1], v[2]) end -- This will result: -- aa, 10 -- cc, 5 -- bb, 2 function getKeyFromVal(name) if (not name) then return false end for i, v in pairs(tkill) do if (v[1] == name) then return i end end return false end function addkill(name, num) if (not name or not num) then return false end local key = getKeyFromVal(name) tkill[key][2] = tkill[key][2] + num end -- we add 6 kill to the "bb" addkill("bb", 6) -- sort again table.sort(tkill, function (a, b) return a[2] > b[2] end) for i, v in pairs(tkill) do print(v[1], v[2]) end --This will result -- aa, 10 -- bb, 8 -- cc, 5 Edited November 24, 2022 by NeverGiveup You gone learn today. I work my ass off, but I still can't pay tho.
Snow-Man Posted November 24, 2022 Author Posted November 24, 2022 @roaddogHello, thank you for helping i have used your example, it sorts the table from data i mean this tkill = {{"aa", 10}, {"bb", 2}, {"cc", 5}} but when add more data, it doesnt sort them, i have already made this to sort function sortKills() table.sort(topKillers, function (a, b) return a[2] > b[2] end) return topKillers end and i tried to get the top killers on dx text like the following code for i, v in pairs ( sortKills() ) do slot = slot + 1 if slot <= 5 then xy = xy + 51 textKill = ("( "..tostring( v[2] ).." ) | "..tostring(( v[1] )) ) or "( N/A ) | N/A" dxDrawRectangle(8, 282+xy, 182, 49, tocolor(0, 0, 0, 175), false) dxDrawText(textKill , 9, 282+xy, 191, 331+xy, tocolor(255, 255, 255, 255), 1.25, "default-bold", "center", "center", false, false, false, false, false) end end so, in the final output i get this result ( 10 ) | aa ( 5 ) | cc ( 2 ) | bb ( 7 ) | Ben USC Informations Head Staff & Owner Of USC. Youtube Channel : My Channel
roaddog Posted November 25, 2022 Posted November 25, 2022 okayy show me how you add the kill to the table it should be check topKillers[key][1] == name and then topKillers[key][2] = topKillers[key][2] + 1 You gone learn today. I work my ass off, but I still can't pay tho.
Snow-Man Posted November 25, 2022 Author Posted November 25, 2022 function addKill(name, num) if (not name or not num) then return false end if (not topKillers[name] ) then topKillers[name] = {name, 0} end local key = getKeyFromVal(topKillers, name) if topKillers[key][1] == name then topKillers[key][2] = topKillers[key][2] + num end end its already the same nothing changed, its only sorts the saved table USC Informations Head Staff & Owner Of USC. Youtube Channel : My Channel
eoL Shady Posted November 25, 2022 Posted November 25, 2022 hello @Snow-Man welcome to the forum, I have prepared a code for your problem and tested it, you can solve your own problem by looking at my codes, I could not understand the code you prepared local tkill = {{"aa", 10}, {"bb", 2}, {"cc", 5}} addCommandHandler("adds", function() local test = {"bla", 404} table.insert(tkill, test) end) addCommandHandler("incr", function() tkill[2][2] = 405 end) addCommandHandler("show", function() table.sort(tkill, function (a, b) return a[2] > b[2] end) for i,_ in ipairs(tkill) do outputDebugString(tkill[i][1].." "..tkill[i][2]) end end) 6 hours ago, Snow-Man said: function addKill(name, num) if (not name or not num) then return false end if (not topKillers[name] ) then topKillers[name] = {name, 0} end local key = getKeyFromVal(topKillers, name) if topKillers[key][1] == name then topKillers[key][2] = topKillers[key][2] + num end end its already the same nothing changed, its only sorts the saved table this way you can constantly update, and do a short solution tests local listUpdater = setTimer(function() table.sort(tkill, function (a, b) return a[2] > b[2] end) end, 300, 0) 1
Snow-Man Posted November 25, 2022 Author Posted November 25, 2022 @Shady1 thank you so much for helping, your example really made me understand the table insert, its really works well now USC Informations Head Staff & Owner Of USC. Youtube Channel : My Channel
eoL Shady Posted November 25, 2022 Posted November 25, 2022 18 minutes ago, Snow-Man said: @Shady1 thank you so much for helping, your example really made me understand the table insert, its really works well now Please, if you have any questions, you can open a new topic and tag me, I will help you.
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