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)
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.
-- 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..