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

How to find the highest value in Number Value?

Asked by 4 years ago

can someone please help me how to find the highest value in number value? Ex: NumberValue1 is 2 NumberValue2 is 5 NumberValue3 is 3 then it prints which is the highest!(Sorry For Bad Grammar)

2 answers

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

Okay, it's a quick explaination.

I will have a value which represents the highest value at the time, I call it "HighestNow".

I will run over the table to check the highest value, take this as an example:

local TableOfValues = {2,5,3}--According to your question.
local function ReturnBiggest(Table){
    local HighestNow = 0
    local ReturnValue = nil
    for key,value in pairs(Table) do--Run through to whole table.
        if value > HighestNow then
            HighestNow = value
            ReturnValue = value--Assume the highest value is this because we don't know.
        elseif value == HighestNow then
            print('There are two values same, just a warning.')
        end
    end
    --We ran the table throughout, now return the value!
    return ReturnValue
}
local rt = ReturnBiggest(TableOfValues)
print(rt)-- Should be 5

Edit: You want it to work, here it is:

local TableOfValueObjects={NumberValueObject1,NumberValueObject2,NumberValueObject3}--According to your situation.
local function ReturnBiggestFromNumberValue(Table){
    local HighestNow = 0
    local ReturnValue = nil
    for key,obj in pairs(Table) do--Run through to whole table.
        if obj.value > HighestNow then
            HighestNow = obj.value
            ReturnValue = obj--Assume the highest value is this because we don't know.
        elseif obj.value == HighestNow then
            print('There are two values same, just a warning.')
        end
    end
    --We ran the table throughout, now return the value!
    return ReturnValue
}
local rt = ReturnBiggestFromNumberValue(TableOfValueObjects)
print(rt)-- Should be some object.
0
thx m8! but can you change those 3 numbers to script.Parent.NumberValue because i did it but didn't work! Ex: local TableOfValues = {Script.Parent.Map1.Value, Script.Parent.Map2.Value, Script.Parent.Map3.Value} it didn't work! ImAnonymousBan 24 — 4y
1
Here you go. LinavolicaDev 570 — 4y
0
Thank you so much! ImAnonymousBan 24 — 4y
0
You can use math.max(), it easier to use loop.https://www.tutorialspoint.com/lua/lua_math_library.htm Block_manvn 395 — 4y
0
Yeah. LinavolicaDev 570 — 4y
Ad
Log in to vote
1
Answered by
Psudar 882 Moderation Voter
4 years ago
Edited 4 years ago
-- t is a table with all your numberValues 
local function sortVals(t)
    local largest = -math.huge
    for _, numberValue in pairs(t) do
        largest = math.max(largest, numberValue.Value)
    end
    return largest
end

probably the easiest way, considering you only want one value.

and much shorter than the solutions before..

0
OmG vouch! thank you so much ImAnonymousBan 24 — 4y

Answer this question