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

How would I get the biggest number?

So lets say you have a list of Intvalues Im trying to get the biggest value of 10 intvalues

right away

does anyone know how I can do that?

1 comment

0

have you heard of iv in pairs

3 answers

A very simple way of doing would be to loop through each object and store each value inside of a table and then call table.sort on it.

Like such:

local folder = Workspace.Folder -- The parent of the objects
local sortedTable = {}
for _,obj in pairs(folder:GetChildren())do
	table.insert(sortedTable,obj.Value)
end

-- This will put the largest value at the end of the table
table.sort(sortedTable)

print(sortedTable[#sortedTable])

1 comment

0

well explained

Following @Kate_Ai’s answer. You could do something like this too.


local sortedTable = {}

forr _,obj in pairs(folder:GetChildren())do
       table.insert(sortedTable,obj)
end

table.sort(sortedTable, function(a,b) return a.Value > b.Value end) —— This will sort the IntValue that’s the highest to be first in the table. It will also be in order from greatest to least.

print(sortedTable[1].Value) ——- Prints the value that’s the largest. Since Lua arrays start at index 1. 1 would be the first and highest int value after sorting it.

1 comment

0

The comments are broken on mobile. Sorry about that.

-- t is a table with all your numberValues 
local function sortVals(t)
	local largest = -math.huge
	for _, numberValue in pairs(t) do
		if numberValue.Value > largest then
			largest = numberValue.Value
		end
	end
	return largest
end

this is probably the best and easiest way since you just want the greatest value.

the reason I use -math.huge is because you might be dealing with negative numbers. if you're never going to though, setting largest = 0 is fine.

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