Getting the intvalue that contain the lowest number

  1. What do you want to achieve? Keep it simple and clear!
    I am making a queue system where player can get a ticket and they will get an Intvalue that have the player name and queue number as it’s value.

  2. What is the issue? Include screenshots / videos if possible!
    All of the Intvalue is contain in a folder and I am trying to get the Getting the intvalue that contain the lowest number so I do something like “print(LowestValue.Name)” So I can get the player name but it is not working at the moment, The error said "ServerScriptService.Main:35: attempt to index nil with ‘Name’ "

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried searching but found nothing yet

	local Values = game:GetService("ReplicatedStorage").TicketFolder
	local AllValues = Values:GetChildren()

	for i, v in pairs(AllValues) do
		AllValues[i] = v.Value
	end
	
	table.sort(AllValues)
	
	local LowestValue = AllValues[1]
	
	print(LowestValue)
	
	status.Value = LowestValue.Name.." is next"
	
	wait(3)

Instead, you could immediately compare values:

local values = game:GetService("ReplicatedStorage").TicketFolder
local allValues = Values:GetChildren()
local lowestValue, lowestValueObject = math.huge, nil -- if you want the IntValue kept

for _, valueObject in pairs(allValues) do
	if valueObject.Value < lowestValue then
		lowestValue = valueObject.Value
		lowestValueObject = valueObject
	end
end

status.Value = LowestValue.Name.." is next"

Oh yeah, there’s one edge case: If the values are equal. Figure that one out. :slight_smile:

Now it is saying “ServerScriptService.Main:67: attempt to compare number < nil” also I think the value equal won’t be a problem because I already sorted that out.

I think you missed a line. PAY ATTENTION!
local lowestValue, lowestValueObject = math.huge, nil -- if you want the IntValue kept

The line is in the script, I think I didn’t missed it?

That’s strange. Alternatively, catch it.

for _, valueObject in pairs(allValues) do
	if not lowestValue then
		lowestValue = math.huge
	end

	if valueObject.Value < lowestValue then
		lowestValue = valueObject.Value
		lowestValueObject = valueObject
	end
end

That is easily circumvented

local function SmallestInteger(IntValues)
    local Result = nil

    for _, IntValue in ipairs(IntValues) do
        if Result == nil or IntValue.Value < Result then
            Result = IntValue.Value
        end
    end

    return Result
end

Either way, this approach is inefficient.

@Fizzavocadoo, you’re far better off using an array. Queues work in FIFO (First In, First Out) order. Address the user who is at the first index of your array and remove them when they’ve been processed. Rinse and repeat this until your queue is empty. From there, it’s as simple as adding clients to the end of your array.

P.S. It might end up being more efficient to erase your array after making your way through the queue, followed by re-allocating the next user to the latest for when adding clients. However, the best approach ultimately varies depending on your intended use of the queue.

1 Like

That worked, but how would I make it return the intvalue object itself and not the value?

In that case, you would make the function return two values instead of one.

return Result, ResultObject

Of course, you have to code the part to handle that which is straight-forward.

1 Like

Thanks for helping everyone, took me quite sometime but I got it.

I feel like you were on the right track with your original code but you probably didn’t know that you can pass a function to table.sort to change the sorting method. math.min would be a nice solution but then you’d have to find which ValueObject is associated with that value so it… falls a little flat.

local values = TicketFolder:GetChildren()

table.sort(values, function(a, b)
    return a:IsA("IntValue") and b:IsA("IntValue") and a.Value < b.Value
end)

local lowestValueObject = values[1]
local lowestValue = lowestValueObject.Value

On another note: if you resolved it on your own without someone else’s help, please do include what exactly you figured out rather than just saying you did it. Others may have the same question and may come across your thread hoping to see how you resolved it and it’d be unfortunate to not find a solution. The forum is a public resource, you can help others out this way too.

1 Like
local function getLowestIntValue(folder)
	local smallestValue, valueObject = math.huge, nil
	for _, intValue in ipairs(folder:GetChildren()) do
		if intValue:IsA("IntValue") then
			if intValue.Value < smallestValue then
				smallestValue = intValue.Value
				valueObject = intValue
			end
		end
	end
	return valueObject
end

Here’s a function which should be fairly simple to implement into your current system. I believe the reason you had issues with the other proposed solutions was caused by the fact that the folder contains instances other than IntValues.

It’s ultimately,

local function SmallestIntValue(IntValues)
    local CurrentSmallest = nil
    local CurrentIntValue = nil

    for _, IntValue in ipairs(IntValues) do
        if CurrentSmallest == nil or IntValue.Value < CurrentSmallest then
            CurrentSmallest = IntValue.Value
            CurrentIntValue = IntValue
        end
    end

    return CurrentIntValue
end

@Fizzavocadoo

table.sort is quicker to write, but operates slower in comparison to a linear search.

Brute-force: O(n)
Sort: O(n log n)