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

How do I retrieve the final result after this loop?

Asked by 4 years ago

I'm trying to get the highest value of the table and I want to make sure its the final value before doing anything with it.

local table = {50,12,78}
local highest = 0


for i,v in pairs(table) do
    if v > highest then
highest = v
end
end
0
you can just write what you want after the loop TheRealPotatoChips 793 — 4y
0
does it save the value? User_3805 6 — 4y
0
Wdym, what do you want to happen after the highest is found AntoninFearless 622 — 4y
0
With the way it's currently set up, "highest" is a variable outside the loop, so you setting it will make it accessible throughout the rest of the script MrLonely1221 701 — 4y
0
ok thanks User_3805 6 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You can use table.sort without the second argument (the second argument is a function of what is sorted (https://developer.roblox.com/en-us/api-reference/lua-docs/table) but the default uses lua operator <.) to rearrange the table and their indexes.

Heres my example:

local a = {4,2,1,3};
table.sort(a)
table.foreach(a,print)
--[[
    This prints:
    1 1
    2 2
    3 3
    4 4
    if 4 is the highest value, therefore
    a[#a] is the highest value since 4 is at the last key.
--]]
print(a[#a])--> 4
Ad

Answer this question