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

How to find the highest value in Number Value?

2 answers

✓ Accepted answer

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

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!

1

Here you go.

0

Thank you so much!

0

You can use math.max(), it easier to use loop.https://www.tutorialspoint.com/lua/lua_math_library.htm

0

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

0

OmG vouch! thank you so much

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