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 5 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!

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

1 answer

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago
local Table = {1,2,3}

local HighestValue = 0
for i,v in pairs(Table) do -- iterate through table
    if v > HighestValue then -- check if value in table is greater than the highest value so far
        HighestValue = v -- sets highestvalue if it his greater than
    end
end
print(HighestValue) -- prints 3 (the highest number) to output

You could also make it into a function

function GetHighestNumber(Table)
    local HighestValue = 0
    for i,v in pairs(Table) do
        if v > HighestValue then
            HighestValue = v
        end
    end
    return HighestValue
end

local Table1 = {1,2,3}
local HighestNumber = GetHighestNumber(Table1)
print(HighestNumber) -- Prints 3
0
Good work. AswormeDorijan111 531 — 5y
Ad

Answer this question