How to loop through a table with a certain order

Order:

local data = {
	["test1"] = 1;
	["test2"] = 10;
	["test3"] = 20;
	["test4"] = 50;
	["test5"] = 100;
}

In this case I’d like to loop from the highest number being 100 (this is supposed to be an rng system) but I just want to know how to always do this order cuz everytime I loop its a different order.

3 Likes

You need to first extract the keys and sort them based on their corresponding values.

  • Extract the keys: The keys table is populated with all the keys from the data table.
  • Sort the keys: The keys table is sorted based on the values in the data table, in descending order.
  • Loop through the sorted keys: The sorted keys are then used to loop through the data table in the desired order.
-- Your table
local data = {
    ["test1"] = 1,
    ["test2"] = 10,
    ["test3"] = 20,
    ["test4"] = 50,
    ["test5"] = 100,
}

-- Extract the keys
local keys = {}
for key in pairs(data) do
    table.insert(keys, key)
end

-- Sort the keys based on their corresponding values in descending order
table.sort(keys, function(a, b)
    return data[a] > data[b]
end)

-- Loop through the table in the sorted order
for _, key in ipairs(keys) do
    print(key, data[key])
end
3 Likes

What you are looking for is weighted chance system, not RNG, there is algorithm for that: Weighted Chance System

1 Like

Whats the difference between this and the link you sent?

This is my original code:

local DB = require(game:GetService("ServerStorage"):WaitForChild("MaterialDatabase"))

local data = {
	["test1"] = 1;
	["test2"] = 10;
	["test3"] = 20;
	["test4"] = 50;
	["test5"] = 100;
}

local function RNGSystem()
	
	for reward, odds in pairs(data) do
		
		print(reward)
		
		local rng = math.random(1,odds)
		
		if rng == odds then
			
			print(reward .. " chosen.")
			
		end
		
	end
	
end

while true do
	
	RNGSystem()
	
	task.wait(2)
	
end
1 Like

I didn’t saw it before, this algorithm is efficient, soo i used it when needed

NOTE: Use Random.new() instead of math.random, it’s up to date, math.random is old

Well whats the difference between mine and the one you linked? Also, the reason I asked how to sort from descending order is cause I want the rng to be determined from the highest possible first bc if I let it roll randomly then it would just pick the lowest weight prize.

1 Like

i didn’t saw you want RNG, also i use this weight system algorithm, it’s used in many engines, if you want to loop in order, first loop should sort keys like you want, and then loop again

What’s the difference between weighted chance and rng

1 Like

In dumb man’s terms:
RNG - completely random, usually used in RNG games (shocker)

Weighted Chance - more biased (certain things have higher chance of being picked compared to another thing), usually used in murder games e.g. MM2 where a player has an X.X% chance of being picked.

You’re going to want to look into table.sort. It will allow you to sort the table from greatest to least or anything really.

Edit: The compression argument will handle the sorting. If you want it implement the rng, then you’ll need to use the weighted system @Axmos mentioned.

1 Like

Are you calling me dumb?

Extra text…

1 Like

LOL no, I said “in dumb man’s terms” that means in simplest form.

Oh ok just wanted to make sure👍