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

How do I find the biggest number/value between a couple of number values?

Asked by
1GlF 42
6 years ago

So I'm making a map voting system and the votes are counted as NumberValues for each map but I don't know how to like, get the biggest value/number.

4 answers

Log in to vote
1
Answered by
trecept 367 Moderation Voter
6 years ago
Edited 6 years ago

If you have more than two values, to find the biggest value you can use math.max.

math.max(100, 20, 30, 5, 500)

This would return 500, the biggest value in the group of numbers. You can replace these numbers with the NumberValue's values.

0
aight thanks ill try it later 1GlF 42 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Hi awesomejoker248674, comparing values is not as hard as you might think.

To find if a value is larger than another, compare them like so;

Local Value1 = 5
Local Value2 = 1

if Local Value1 > Value2 then
    print("Value1 is larger than Value2")
else
    print("Value 1 is smaller than Value2"

end

The output SHOULD say;

"Value1 is larger than Value2"

If it doesn't, I recommend re-installing your studio, because that's not right at all XD

0
the problem is, i have 3 values 1GlF 42 — 6y
0
Oh thats easy, just use math.max EtherealTrin 45 — 6y
Log in to vote
0
Answered by
zblox164 531 Moderation Voter
6 years ago
Edited 6 years ago

You can use math.max() to find the greatest value and math.min() to find the lowest value.

Log in to vote
0
Answered by
WXBZ 3
6 years ago

By the way you are wording it, it would seem that you are using actual value instances, so I suggest this as it will return the value as well as the object. This also works if multiple instances have the same value since it will be saved to a table.

function FindHighestVotes(Folder)
    local HighestVotes = {{nil}, 0}
    for _, ValueObj in pairs(Folder:GetChildren()) do
        if ValueObj.Value > HighestVotes[2] then
            HighestVotes[1] = {ValueObj}; HighestVotes[2] = ValueObj.Value
        elseif ValueObj.Value == HighestVotes[2] then
            table.insert(HighestVotes[1], ValueObj)
        end
    end
    return HighestVotes
end

for _, Map in pairs(FindHighestVotes(workspace.Values)[1]) do
    print(Map.Name)
end

Answer this question