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

How can i compare table elements among themselves?

For example. i have a table

t = {Variant1 = 33, Variant2 = 1, Variant3 = 123, Variant4 = 33}

I need to find the key(s) with highest value(s). So, how can i compare it?

2 answers

✓ Accepted answer

This is how I always do it.

local highest = 0
local t = {Variant1 = 33, Variant2 = 1, Variant3 = 123, Variant4 = 33}

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

print(highest)

2 comments

0

But what if i have 2 same highest? for example var1 = 2, var2 = 5, var3 = 5. I need to randomize the choose between same numbers.

1

then you can add them to a table and then math.random(1,#Table

This is the most(maybe) solution I can found:

local t ={1, 2, 3}

print(math.max(unpack(t)))

About the randomize, why you need to randomize it when it already the same?

1 comment

0

Not sure if i need exactly this method, but seems it's useful in other situations, so i'll remember it, thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.