So I'm trying to make a piece of code, that basically says 3 numbers in a table, and I want the script to print the highest number. I am really confused so if someone could help me that'd be great!
1 | local Table { 1 , 2 , 3 } |
2 | for i,v in pairs (Table) do |
3 | print (Table.MaxValue) |
4 | end |
1 | local Table = { 1 , 2 , 3 } |
2 |
3 | local HighestValue = 0 |
4 | for i,v in pairs (Table) do -- iterate through table |
5 | if v > HighestValue then -- check if value in table is greater than the highest value so far |
6 | HighestValue = v -- sets highestvalue if it his greater than |
7 | end |
8 | end |
9 | print (HighestValue) -- prints 3 (the highest number) to output |
You could also make it into a function
01 | function GetHighestNumber(Table) |
02 | local HighestValue = 0 |
03 | for i,v in pairs (Table) do |
04 | if v > HighestValue then |
05 | HighestValue = v |
06 | end |
07 | end |
08 | return HighestValue |
09 | end |
10 |
11 | local Table 1 = { 1 , 2 , 3 } |
12 | local HighestNumber = GetHighestNumber(Table 1 ) |
13 | print (HighestNumber) -- Prints 3 |