# [SOLVED] How to find lowest and highest int value?

Hi developers! I need help with scripting.

Imagine that we have 3 int values.

• Int value 1(Value = 1)
• Int Value 2(Value = 2)
• Int Value 3(Value = 3)

In coding, How can I found out which of the int values is highest or lowest?

Like:

``````local Values = game.ReplicatedStorage.ValuesFolder
local AllValues = Values:GetChildren()
local HighestValue = ... -- That's my question.
local LowestValue = ... -- That's my question too.
print("The Lowest IntValue Is".. LowestValue)
print("The Highest IntValue Is".. HighestValue)
``````

I will be happy if someone explains that.

2 Likes

assuming all children are IntValues, you can try this

``````local Values = game.ReplicatedStorage.ValuesFolder
local AllValues = Values:GetChildren()

for i, v in pairs(AllValues) do
AllValues[i] = v.Value
end
table.sort(AllValues)

local HighestValue = AllValues[#AllValues]
local LowestValue = AllValues[1]
``````
1 Like

Ok, Let me try. Thanks for reply.

2 Likes

It Worked! Thank you so much!!!

2 Likes

Pro tip, table.sort takes a function as itâ€™s second argument, so instead of transforming the table first, if you still wanted to go the sorting route, you can do this:

``````local sorted = table.sort(values, function(a, b) return a.Value < b.Value end)
``````
1 Like

as that is a good pro tip, there isnâ€™t a need for the second function parameter if it does least to greatest by default

that is why I didnâ€™t show it in the code

FYI: easier way to do this is just to use math.min and math.max after unpacking the table. You donâ€™t necessarily need to sort the table and then access the first and last indices. min and math respectively return the minimum or maximum (lowest or highest) value of the arguments it receives.

``````local valuesFolder = game:GetService("ReplicatedStorage").ValuesFolder

local values do
local intValues = valuesFolder:GetChildren()
values = table.create(#intValues)

for _, intValue in ipairs(intValues) do
table.insert(values, intValue.Value)
end
end

print(math.min(table.unpack(values))) --> lowest value
print(math.max(table.unpack(values))) --> highest value
``````
5 Likes

I wouldnâ€™t say it is easier, if anything it is more complicated then my solution

• First, we define the folder where our values are held.

• We create a variable that will hold the values of the IntValues we collect from the ValuesFolder and then start a new scope. This scope is to help keep some variables that we will not need later, such as the list of children. We donâ€™t want that hanging around!

• We create a table with a size of the number of children in the ValuesFolder. This is an â€śoptimisationâ€ť to an extent as we preallocate the size and memory for the table before filling it with its elements. If we donâ€™t do this, the table will reallocate every time we add a new element because tables created with the default constructor {} allocate with a size of 0.

• We iterate through the children of the ValuesFolder and then insert their values into our values table. We then close the scope, cleaning up any local variables that are no longer relevant to the script. We also free up some local registersâ€¦ not like youâ€™d hit the maximum though!

• Next up is just two prints implementing both math.min and math.max to respectively collect the lower and upper bounds of the collected values. We have to use table.unpack so these functions are fed varargs because it does not take a table of arguments, rather a tuple.

Itâ€™s pretty simple, efficient and doesnâ€™t incorporate anything unnecessary like sorting. Itâ€™s really just as simple as a simple less than or greater than check across all the fed values and whichever value meets the target criteria as the final survivor gets printed out.

2 Likes

I said it was more complicated then my solution, I never said it was complicated

your solution has more steps making it more complicated
I am not saying your solution is bad, but I donâ€™t see the need for this huge reply over a small thing I said like come on man

besides let me do what your code does using my solution

``````local ValuesFolder = game.ReplicatedStorage.ValuesFolder
local AllValues = ValuesFolder:GetChildren()
local Values = {}

for i, v in pairs(AllValues) do
Values[i] = v.Value
end

print(math.min(table.unpack(Values))) --> lowest value
print(math.max(table.unpack(Values))) --> highest value
``````

I tried making do what youâ€™re does, it has a new values table and the values are inserted into it
I also use what you did in the prints

besides you explain it like I donâ€™t know what the code does, have a little faith in me

EDIT: in the first part I donâ€™t mean it is more complicated then the code in this reply, talking about the one that was marked as the solution

Yeahâ€¦ quoting from the dictionary comes off as obnoxious, please donâ€™t do that. Iâ€™m not going by the direct definition but rather just a colloquial reference of the word. There wasnâ€™t really a need to reply calling it complicated, but since you did, I believed others may find it complicated as well so I took the time to explain it just in case and show you what it does.

Having more lines, or â€śstepsâ€ť, doesnâ€™t necessarily make something more complicated, I just simply want to account for certain bases that would otherwise not be considered like managing local variables that are only used for specific cases and are irrelevant to the rest of the script.

As for the code sample you provided, besides my own personal nitpick of using pairs instead of ipairs to iterate an array (the latter is faster and more idiomatic), they both do the same thing. Less lines doesnâ€™t necessarily mean less complicated, better or anything, really. The only difference is that youâ€™ve opted to remove my variable scoping and use the indice for GetChildren as one for the new array. Some of the (micro)optimisations Iâ€™ve made were also ditched.

Dunno, it just seems pointless to nitpick my code like this unless there was an actual problem with it, besides trying to flare an argument for the sake of it.

2 Likes

but I didnâ€™t say it was complicated for me, I was saying it was more complicated then the solution
and no you keep saying that I think that because it has less lines, which is false

I said it was not as complicated because you create a whole new variable and create a whole new table which is more steps, besides your code is better then mine I just wanted to tell you that it wasnâ€™t easier
anyways who cares if I bring definitions, they prove points

Iâ€™m just going to stop replying after this, letâ€™s just agree to disagree ig

Irrespective of where the â€ścomplicatedâ€ť comment was directed to, whether if it was about the code in general or in comparison to your comment, no, the level of complication does not differ in any sense of the word. It just employs some optimisations and good practices that you can also follow, or not.

Mind you, both the new variable and the new table are equivalent to the GetChildren call used in the original solutionâ€™s code as well as the new table used to collect the values of the IntValues, so if thatâ€™s the point of complication then the code samples and their declarations are the exact same except the array generated by GetChildren is garbage collected when it is no longer needed. This way there are only two variables actually left on the stack: the reference to the ValuesFolder as well as an array of numbers that were collected from each IntValue.