How to loop a dictionary wth a proper order?

I was scripting rarity system. And then I noticed that for i,v in pairs do loops in a chaotic order. And this ruins the whole probability up
However using ipairs isn’t the solution either. Because ipairs can’t loop through a dictionary. Same thing applies to for i,v in table(dictionary im my case)

image10
This is the proper order
image11
And ths is the order it goes through

dictionaries are an unordered data structure, the key value pairs are not stored in the order you set them

1 Like

ipairs can loop through a dictionary no?

you just print key and then the value after it

ohhh nvm that’s pairs my bad charrr

It can’t, i added a print check and it doesnt

hmmm

i’m not sure what you’re trying to use that dictionary for

but perhaps wherever you call it, you could index it with whatever you need so it prints out the correct key and value?

i can’t really get a different idea rn

To get them in order you could try sorting them like this

local Rarities = {
	[1] = {
		["Content"] = "Fire",
		["Rarity"] = 1,
	},
	[2] = {
		["Content"] = "Water",
		["Rarity"] = 24,
	},
	[3] = {
		["Content"] = "Earth",
		["Rarity"] = 25,
	},
	[4] = {
		["Content"] = "Air",
		["Rarity"] = 25,
	},
}

for i,v in Rarities do

	print(v["Content"] .. " " .. tostring(v["Rarity"]))

end
2 Likes

tysmmmm it worked, lemme say some stuff so the post has enough chars