Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I retrieve the final result after this loop?

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

5 comments

0

you can just write what you want after the loop

0

does it save the value?

0

Wdym, what do you want to happen after the highest is found

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

0

ok thanks

1 answer

✓ Accepted answer

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.