How'd I get the 5 biggest values inside a table?

The title is self explanatory. I want to get the 5 biggest values out of my table, and here’s what my table looks like.

clickMultis[i] = {
  ["clickMultiplier"] = (folder.ClickMultiplier1.Value+folder.ClickMultiplier2.Value)/2,
  ["petID"] = folder.PetID.Value
}

This is placed in a for loop, so it’s basically gonna be indexing by numbers. The pet ID is just used for finding the pet inside the player, to then equip it. The click multiplier is the number inbetween the two set values. The for loop makes one entry per pet.

Quick summary: I want to get the 5 biggest click multipliers of this table, how should I do it?

You’ll have to use data stores. So ordered data stores. If you need me to write an example then I’ll be willing too.

There must be some way to do it without that, as it shouldn’t require data stores, that sounds weird.

It’s how you order stuff. It’s the most reliable way. It’s fairly simple once you get the hang of it. This is used to get leaderbaords and stuff. Because it sorts the biggest(or smallest) int values. And if you want to sort the top five you’ll have to do so. I can write an example if you want.

The reason I wouldn’t prefer this is because of Roblox limits (ratelimits), which would make it pretty inefficient.

you don’t need to use datastores for something like this

I mean like ordered data stores. Tbh i’ve never made something like this before. So I don’t really know how you would. I’m just guessing.

you can do something like this:

local highestNumber = 0

for _, v in pairs(clickMultis) do
	if v.clickMultiplier > highestNumber then
		highestNumber = v.clickMultiplier
	end
end

print("Highest number ", highestNumber)

This would get one value that is the highest. But how about I put this in a for loop and remove the one I found from the table, that should get me the very highest, correct?

1 Like

Yeah, that would work as well.

1 Like

You can table.sort it from highest to lowest then remove all values but the first 5

1 Like

You could sort the table from greatest to least and get the first 5 values.

Example:

local table1 = {5, 76, 9, 0, 47, 19, -1, 92}

table.sort(table1, function(a, b)
    return a > b
end)

print(table1[1] ,table1[2] ,table1[3] ,table1[4] ,table1[5])
-- expected output: 92, 76, 47, 19, 9

I guess you didn’t look at how my tables look. This wouldn’t work.

The OP uses dictionaries and table.sort does not work on dictionaries

clickMultis[i] = {
	["clickMultiplier"] = (folder.ClickMultiplier1.Value+folder.ClickMultiplier2.Value)/2,
	["petID"] = folder.PetID.Value
}

table.sort(clickMultis[i], function(a, b)
	return a[2] > b[2]
end)

for i, v in pairs(clickMultis[i]) do
	if i <= 5 then
		print(v)
	end
end

Then the title should not be “How’d I get the 5 biggest values inside a table?”, rather it should be “How’d I get the 5 biggest values inside a dictionary?”

a dictionary is a table
the title isn’t wrong

1 Like
-- Table extentions. So sad that lua has a terribly small set of built in functions.

-- This iterator function returns n amount from t table.
function TakeEnum(t, amount)
	local i = 0
	return function()
		if (i < amount) then
			i = i + 1
			return i, t[i]
		end
	end
end

local values = { 1,4,7,3,9,10,8,6,2,5 } -- This is an array table. Change it to that first.

table.sort(values, function(a,b)
    return a > b
end)

for k,v in TakeEnum(values, 5) do
    print(v)
end

The problem with this script is that it only eats array tables, not associative. Convert clickMultis into that first.
I don’t know how you structured your whole thing so i can’t help you with that.
I’m fresh out of lua school, with experience of 2 months so I’m not sure if this is the proper way of doing things. Let others critique before you use it.

The simplest way is use table.sort and then take the first 5 values of a table