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)
How to find the highest value in Number Value?
2 answers
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.
5 comments
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!
Here you go.
Thank you so much!
You can use math.max(), it easier to use loop.https://www.tutorialspoint.com/lua/lua_math_library.htm
Yeah.
-- 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..
1 comment
OmG vouch! thank you so much
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Possible duplicate of: https://scriptinghelpers.org/questions/89216/how-would-i-get-the-biggest-number#82666