|
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
| |
|
|
| 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
|
  |
| Joined: 13 Nov 2009 |
| Total Posts: 1911 |
|
|
| 09 Jun 2012 09:13 PM |
@flappy What do you think those are? |
|
|
| Report Abuse |
|
|
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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|