• Sorting leaderboard
    9 replies, posted
[code] function ENT:Draw() cam.Start3D2D(self:GetPos() - self:GetAngles():Forward() * 190 + self:GetAngles():Right() * 190, self:GetAngles() + Angle(90, 90, 90), 0.3) draw.RoundedBox(3, 0, 0, 1270, 50, Color( 100, 0, 0, 255 )) draw.RoundedBox(3, 0, 70, 1270, 50, Color( 100, 0, 0, 255 )) draw.DrawText(Hostname, "HostFont", 5, 0, Color( 255, 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) draw.DrawText("Name Kills Rounds Won", "HostFont", 5, 70, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) for k,v in pairs(player.GetAll()) do draw.RoundedBox(3, 0, Slots[k], 675, 50, Color( 204, 0, 0, 255 )) draw.RoundedBox(3, 680, Slots[k], 195, 50, Color( 204, 0, 0, 255 )) draw.RoundedBox(3, 885, Slots[k], 195, 50, Color( 204, 0, 0, 255 )) draw.RoundedBox(3, 1090, Slots[k], 180, 50, Color( 204, 0, 0, 255 )) draw.DrawText(v:Name(), "PlayerFont", 5, Slots[k], Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) draw.DrawText(v:GetNetworkedInt( "GameKillsInt", 0 ), "PlayerFont", 760, Slots[k], Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) draw.DrawText(v:GetNetworkedInt( "GameRoundsInt", 0 ), "PlayerFont", 960, Slots[k], Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) draw.DrawText(v:GetNetworkedInt( "GameRoundsWonInt", 0 ), "PlayerFont", 1160, Slots[k], Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) end cam.End3D2D() end [/code] I am looking to sort players on this leaderboard from the highest NetworkedInt to lowest, the NWInts are [code] v:SetNWInt("GameRoundsInt", v:GetPData("GameRounds")) v:SetNWInt("GameRoundsWonInt", v:GetPData("GameRoundsWon")) v:SetNWInt("GameKillsInt", v:GetPData("GameKills")) [/code] I can't figure it out, any help would be appreciated.
You can loop through the table, or use table.sort
[QUOTE=Kevlon;50197225]You can loop through the table, or use table.sort[/QUOTE] I have [code] local PLAYERS = player.GetAll() table.sort( PLAYERS, function( a, b ) return a:GetNWInt( "GameKills" ) > b:GetNWInt( "GameKills" ) end ) [/code] How can I implement this though? Sorry for my ignorance on this subject.
Just loop through the PLAYERS table rather than the player.GetAll() table
[QUOTE=MPan1;50197283]Just loop through the PLAYERS table rather than the player.GetAll() table[/QUOTE] Like [code] local SortTable = table.sort( PLAYERS, function( a, b ) return a:GetNWInt( "GameKills" ) > b:GetNWInt( "GameKills" ) end ) for k,v in pairs(SortTable) do [/code] this? obviously not right but I don't know much about tables.
:snip: didn't work
[QUOTE=MPan1;50197305]I think [CODE] local PLAYERS = player.GetAll() table.sort( PLAYERS, function( a, b ) return a:GetNWInt( "GameKills" ) > b:GetNWInt( "GameKills" ) end ) for k,v in pairs(PLAYERS) do [/CODE] Would work [editline]25th April 2016[/editline] Then again, sorting is usually a pain in the ass and doesn't work[/QUOTE] Yeah, that didn't work. Is there any other way I could sort it? Guessing not
Perhaps [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/ipairs]ipairs[/url] would work [editline]25th April 2016[/editline] Actually, it probably won't, ignore me
[QUOTE=Shenesis;50197635]It isn't working because you are getting the wrong NWInt: You are trying to get the NWInt called [B]GameKills[/B], but only the NWInt [B]GameKillsInt[/B] exists, as you have shown in the OP. GameKills is the PData ID while GameKillsInt is the NWInt ID, don't mix them up. As such, MPan1's solution was correct, but the wrong value was being retrieved (and so, it defaulted to 0 since the NWInt was not found); here's what you should have: [lua] local PLAYERS = player.GetAll() table.sort( PLAYERS, function( a, b ) return a:GetNWInt( "GameKillsInt" ) > b:GetNWInt( "GameKillsInt" ) end ) for k,v in pairs(PLAYERS) do [/lua][/QUOTE] This makes no players called at all, just blank. Odd.
Sorry, you need to Log In to post a reply to this thread.