I,v loops missing out values

Hello there I am scripting a case opening system. Somehow whenever the i,v loop goes through the table it will always not compare the random number with 4(epic) and 25(uncommon) thus making it impossible to get them.Any help is appreciated
script:

PurchaseTrail = function(plr,CaseName)
		local Chances = require(game.ServerScriptService.ItemChances)
		local Price = game.StarterGui.MainGui.ShopAsset.TrailShop.Frame:FindFirstChild(CaseName).Price
		local Tokens = plr.PlayerValues.Tokens
		local case = game.ReplicatedStorage.Trails:FindFirstChild(CaseName)
		local returned =""
		if Tokens.Value >= Price.Value then
			local Table  = Chances.NormalChances
			local randomNumber = math.random(0,100)
			local tab = {}
			for i,v in pairs(Chances.Chances) do
				print(randomNumber)
				print(i)
				print(randomNumber < i)
				if randomNumber < i then do
						local picked = v
						local inv = plr.OwnedTitles
						for i,v in pairs(case:GetChildren()) do
							if v.Rarity.Value == picked then 
								table.insert(tab,v)
							else 
							end
						end
						local pick = tab[math.random(1,#tab)]
						local new = Instance.new("BoolValue")
						new.Name = pick.Name
						new.Parent = inv
						local rarityItem = Instance.new("BoolValue")
						rarityItem.Name = pick.Rarity.Value
						rarityItem.Parent = new
						Tokens.Value = Tokens.Value - Price.Value
						print(pick)
						return pick
					end

				end
			end
		end
	end,

Table Script:

local module = {
	 Chances = {
		[1] = "Legendary" ,
		[4] = "Epic",
		[10] = "Rare" ,
		[25] = "Uncommon",
		[101] = "Common"
	}
}

return module

Dictionaries don’t have an order to them, so this might be iterating out of order.

yea but why did the i,v loop not loop through the whole dictionary

Because on this line you’re ending the loop. After the loop ends, it won’t check anything else.

1 Like

As @JarodOfOrbiter said, dictionaries have no order. If you did a simple print(i) you would notice that it doesn’t print in the order you expect. Instead it prints in the order of 1, 10, 101, 25, 4 and obviously the random number of 0-100 will mainly be less than 101, so 25 and 4 are neglected.

2 Likes

is there a way to make the dictionary have a custom order for it to follow?

Convert the dictionary into an array [1], [2], [3] then sort it.

It has been done before if you searched it:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.