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 » Scripting Helpers
Home Search
 

Re: Getting the highest value.

Previous Thread :: Next Thread 
Narutoworl is not online. Narutoworl
Joined: 09 Jul 2008
Total Posts: 2001
09 Jun 2012 09:01 PM
Let's say that there were 5 IntValues, each with a random number. How would you get the IntValue that has the highest number?
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
09 Jun 2012 09:02 PM
`math.max` returns the highest value of its parameters.

    print(math.max(1,5,6,2,1,3,4));

> 6
Report Abuse
Narutoworl is not online. Narutoworl
Joined: 09 Jul 2008
Total Posts: 2001
09 Jun 2012 09:03 PM
I don't think that'll work for me in this situation, I'm having an IntValue in everyone's Player. I want to find which of the player's has the highest value. Not sure how I'd put that into math.max()
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
09 Jun 2012 09:04 PM
Sadly you couldn't unless you modified `math.max`. Use one of these :

http://wiki.roblox.com/index.php/Algorithms#Data_Searching
Report Abuse
flappydavid is not online. flappydavid
Joined: 03 May 2009
Total Posts: 5893
09 Jun 2012 09:05 PM
Or just compare it...
Report Abuse
Narutoworl is not online. Narutoworl
Joined: 09 Jul 2008
Total Posts: 2001
09 Jun 2012 09:07 PM
How would I compare them? Right now I'm using GetPlayers, so I have all the Players, but I don't know where to go from there.
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
09 Jun 2012 09:13 PM
@flappy
What do you think those are?
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
09 Jun 2012 10:14 PM
"I don't think that'll work for me in this situation, I'm having an IntValue in everyone's Player. I want to find which of the player's has the highest value. Not sure how I'd put that into math.max()"

num = 0


for _, plr in pairs(game.Players:GetPlayers()) do
num = math.max(plr.IntValue.Value, num)
end


----------
    ~pwnedu46, wiki writer~
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
09 Jun 2012 10:16 PM
@pwnedu
That's a linear search however you didn't save the current player and you also didn't define whether or not there was a change...
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
09 Jun 2012 10:17 PM
You can do `if num==plr.IntValue.Value then` if you want to have the tiebreaker go towards the newer player instance. Also using `math.max` over `>` is very inefficient.
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
09 Jun 2012 10:25 PM
"That's a linear search however you didn't save the current player and you also didn't define whether or not there was a change..."
You can tell whether or not there was a change by checking if num == 0. This will tell you the winner.

num = 0
winner = nil

for _, plr in pairs(game.Players:GetPlayers()) do
num = math.max(plr.IntValue.Value, num)
winner = plr.IntValue.Value == num and plr or winner
end



----------
    ~pwnedu46, wiki writer~
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
09 Jun 2012 10:29 PM
> You can do `if num==plr.IntValue.Value then` if you want to have the tiebreaker go towards the newer player instance. Also using `math.max` over `>` is very inefficient.
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
09 Jun 2012 10:32 PM
As with most things, the difference between math.max and ​> is negligible.


----------
    ~pwnedu46, wiki writer~
Report Abuse
orangegreenblue is not online. orangegreenblue
Joined: 04 Sep 2010
Total Posts: 10906
09 Jun 2012 10:53 PM
Values = {}
for i, v in pairs(Game.Players:getChildren()) do
pcall(function() table.insert(Values, #Values + 1, v.Value.Value) end)
end
print(math.max(Values))

-orangegreenblue, the Lua Noob
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
10 Jun 2012 01:29 AM
@pwnedu
In most situations where you would need something like this, the data could range from 100 to 10000 elements in which case it makes a huge difference.
Report Abuse
ScripterJQ is not online. ScripterJQ
Joined: 22 May 2012
Total Posts: 177
10 Jun 2012 01:50 AM
local max=0

for i,v in pairs(workspace.Values:children()) do
if v:isA"IntValue"then
if v.Value>max then
max=v.Value
end
end
end
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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