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
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
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
This discussion is preserved as it was. New answers, comments, and votes are closed.
5 comments
you can just write what you want after the loop
does it save the value?
Wdym, what do you want to happen after the highest is found
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
ok thanks