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

How would I get the biggest number?

Asked by 5 years ago

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?

0
have you heard of iv in pairs ReallyUnikatni 68 — 5y

3 answers

Log in to vote
0
Answered by
ScuffedAI 435 Moderation Voter
5 years ago

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])
0
well explained EmbeddedHorror 299 — 5y
Ad
Log in to vote
0
Answered by
Alphexus 498 Moderation Voter
5 years ago
Edited 5 years ago

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.
0
The comments are broken on mobile. Sorry about that. Alphexus 498 — 5y
Log in to vote
0
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
        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.

Answer this question