[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?


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.

Thank you for reading. :star:


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

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

Ok, Let me try. Thanks for reply.


It Worked! Thank you so much!!!


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)

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

I wouldn’t say it is easier, if anything it is more complicated then my solution

Anything you find complicated about this? I can break it down for you:

  • 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! :sweat_smile:

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


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

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 :sweat_smile:

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. :stuck_out_tongue:

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.


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.

There’s… nothing complicated about this code, in comparison to anyone else’s or in general. :slight_smile: