What do i use to determine which four different int values are higher? and choose that value?
i tried doing for loops i just can't wraped my head around.
I'm no rocket scientist, but I'm pretty sure you can just use math.max for this sort of thing.
1 | local highest = math.max( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) --//You can put whatever numbers you want in there. |
2 |
3 | print (highest) --//Will output 9 |
You can also put the numbers inside of a table, and using the unpack function can unload them into the function like this.
1 | local numbers = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } |
2 | local highest = math.max( unpack (numbers)) --//Is the same as the code from before, but this time we're taking all the values from the table and unpacking them into the math.max function. |
3 |
4 | print (highest) --//Still outputs 9 |
Not sure if I'm reading the question wrong or not judging from the above answers, but the math.max function can certainly get the largest number from a group of numbers. Conversely there is the math.min function, which does the same thing but returns the smallest number.
This is quite simple.
01 | local function SortIntValues(folder) |
02 | local intValues = { } |
03 | for i,v in pairs (folder:GetChildren()) do |
04 | table.insert(intValues, v) |
05 | end |
06 | table.sort(intValues, function (a,b) return a.Value > b.Value end ) |
07 | return intValues [ 1 ] |
08 | end |
09 |
10 | local HighestIntValue = SortIntValues(folderWithIntValues) — This is the highest int value. |
I'm kind of tired right now so if there is a predefined method for this somebody tell me, but here is a function you can use:
01 | local one = 1 |
02 | local two = 2 |
03 | local three = 3 |
04 | local four = 4 |
05 |
06 | local values = { |
07 | one, |
08 | two, |
09 | three, |
10 | four, |
11 | } |
12 |
13 | function getHighest(table) |
14 | highest = 0 |
15 | for i,v in pairs (table) do |