Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I'm having a bit of problems. Can someone point me in the right direction?

Asked by 6 years ago

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!

1local Table {1,2,3}
2for i,v in pairs(Table) do
3print(Table.MaxValue)
4end
0
missing equals on line 1 greatneil80 2647 — 6y

1 answer

Log in to vote
3
Answered by 6 years ago
Edited 6 years ago
1local Table = {1,2,3}
2 
3local HighestValue = 0
4for 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
8end
9print(HighestValue) -- prints 3 (the highest number) to output

You could also make it into a function

01function 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
09end
10 
11local Table1 = {1,2,3}
12local HighestNumber = GetHighestNumber(Table1)
13print(HighestNumber) -- Prints 3
0
Good work. AswormeDorijan111 531 — 6y
Ad

Answer this question