generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How could math.max() help in this case?

Previous Thread :: Next Thread 
Salinas23 is not online. Salinas23
Joined: 28 Dec 2008
Total Posts: 37141
13 Dec 2016 05:34 PM
Let's say I want to check who's the player (i.e. at the end of the game) with the highest number of Knockouts e.e

I have a spider on my back.
Report Abuse
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
13 Dec 2016 05:36 PM
math.max could only give you the highest number but it wouldn't tell you who has it

instead i would just use table.sort on a table and take the highest value from there
Report Abuse
Mister_Manakin is not online. Mister_Manakin
Joined: 16 Sep 2009
Total Posts: 1935
13 Dec 2016 05:37 PM
function getMax(list)

local max = list[1]

for i,v in pairs(list) do
max = math.max(max,list[i])
end

return max

end
Report Abuse
Salinas23 is not online. Salinas23
Joined: 28 Dec 2008
Total Posts: 37141
13 Dec 2016 05:42 PM
local t = {
["Salinas23"] = 3,
["ur mom"] = 8,
["Telamon"] = 23,
["ROBLOX"] = 9,
["my mom"] = 11}

table.sort(t)

for key, value in ipairs (t) do
print(key .. ": " .. value)
end

This doesnt print anything... What am I doing wrong? obv no output

I have a spider on my back.
Report Abuse
Salinas23 is not online. Salinas23
Joined: 28 Dec 2008
Total Posts: 37141
13 Dec 2016 05:44 PM
Oh true, I wrote it as an ipair.
Thing is, using 'pairs' wont print it in order:/

I have a spider on my back.
Report Abuse
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
13 Dec 2016 05:46 PM
yea for me it does but it prints it from smallest to largest and not the other way around
Report Abuse
Salinas23 is not online. Salinas23
Joined: 28 Dec 2008
Total Posts: 37141
13 Dec 2016 05:52 PM
@Mister

had to read that 3 times to understand it nicely. Thanks.

I have a spider on my back.
Report Abuse
FlamedSkull is not online. FlamedSkull
Joined: 23 May 2009
Total Posts: 1379
13 Dec 2016 05:55 PM
local Table = {["One"] = 1,["Two"] = 3,["Three"] = 3}

table.sort(Table, function(a,b) return a>b end)

print(table.concat(Table," "))


Report Abuse
coolandy31 is not online. coolandy31
Joined: 03 Sep 2014
Total Posts: 109
13 Dec 2016 05:57 PM
table.concat and table.sort work only with arrays im pretty sure not dictionarys



Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
13 Dec 2016 05:58 PM
dictionaries dont have a length so you cant sort it.

@OP just sort the array

local players = game.Players:GetPlayers()

table.sort(players,function(p1,p2)
return p1.leaderstats.Kills.Value > p2.leaderstats.Kills.Value
end)

for i,v in next, players do
print(i,v.Name)
end


Report Abuse
Salinas23 is not online. Salinas23
Joined: 28 Dec 2008
Total Posts: 37141
13 Dec 2016 06:17 PM
@time

that would work perfectly for a complete leaderboard but I need to know how many (and which) players had the highest number of 'Kills' and 'reward' them.

I have a spider on my back.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
13 Dec 2016 06:22 PM
i literally just showed you. i dont understand wat else you want??


Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
13 Dec 2016 06:24 PM
whatever way you keep track of the values for each player, just sort the table afterwards like i gave


Report Abuse
CeaselessQuokka is not online. CeaselessQuokka
Joined: 03 Jul 2012
Total Posts: 7841
13 Dec 2016 06:24 PM
@Salinas, sorting does just that, you can get as many players you want with the highest KOs or whatever other stat, and it says which.

local players = game:GetPlayers();
table.sort(players, function(value1, value2)
return value1.leaderstats.KOs > value2.leaderstats.KOs;
end);

Now, the array is sorted from player with the highest KOs to the player with the least KOs.

You have the player, the stat, and you can "reward" them.


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image