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

Find the highest value on a table?

Asked by 6 years ago

I could easily find this on roblox forums, too bad those idiots removed them. Anywho, how would I print the highest value on a table of numbers?

0
The ROBLOX Staff built the entire ROBLOX platform for you to make your games on, I'd advise you not to call them "idiots". Eqicness 255 — 6y
0
That was an essential part of ROBLOX that everyone enjoyed and has been there forever. It's worse than when they removed tix lmao gmatchOnRoblox 103 — 6y
0
^ LoganboyInCO 150 — 5y
0
bro yall are actual cringe User#22145 0 — 3y

2 answers

Log in to vote
1
Answered by 6 years ago
local highest = 0
local nums = {5,10,105,35,2,.1,401}

for i,v in pairs(nums)do
    if v > highest then
        highest = v
    end
end

print(highest)

This makes a variable called "highest" which is set to 0 (could set it lower if you're getting the highest of negative numbers) then iterates through the table containing numbers and sets the value of highest to the variable if it is higher than the last.

Ad
Log in to vote
0
Answered by
Eqicness 255 Moderation Voter
6 years ago
Edited 6 years ago

The best way to do this would be Iteration with a For loop. Here's an example:

local min = 0 -- Use this to set your minimum number if you want, totally up to you.
local numbers = {1, 2, 3, 4, 5}

function findHighest(table)
    local highest = 0
    for i, n in pairs(table) do
        if n > min then
            min = n
            highest = n
        end
    end
    return highest
end

local highestInNumbers = findHighest(numbers)

You can then use highestInNumbers to reference later.

Answer this question