Rarity system not working 100% correctly

I made a rarity system on roblox but the function wat i made does not work how i wanted to. I wanted to get all the items that have the Tier that i got and get a random item from it but it does not work at all

local Items = {}

Items.data = {
	{
		Name = "Hat";
		ImageID = 1409499909;
		Cost = 100;
		Value = 10;
		Tier = "Commen";
		ItemID = 1
	};
	{
		Name = "Pants";
		ImageID = 128302713;
		Cost = 30;
		Value = 100;
		Tier = "Commen";
		ItemID = 2
	};
	{
		Name = "Shirt";
		ImageID = 495262405;
		Cost = 330;
		Value = 1000;
		Tier = "Uncommen";
		ItemID = 3
	};
	{
		Name = "Glasses";
		ImageID = 1439396148;
		Cost = 2000;
		Value = 1000;
		Tier = "Rare";
		ItemID = 4
	};
	{	
		Name = "Chain";
		ImageID = 1027719587;
		Cost = 1233;
		Value = 4623;
		Tier = "Specail";
		ItemID = 5
	};
}

function Items.Get_Items_FromID(ID)
	for Items,Item in pairs(Items.data) do --// Looping thru module	
		if type(Item)== "table" and Item.ItemID == tonumber(ID) then --// Checking if Item is a table and if ID_Parameter == ID
			return Item --// Returns Item if found
		end 
	end
end

function Items.Crate_Award(Tier)
	for Items,Item in pairs(Items.data) do --// Looping thru module	
		if Item.Tier == tostring(Tier) then --// Checking if Item is a table and if ID_Parameter == ID
			print(Item[math.random(1,#Items)])
			return Item
		end 
	end
end

return Items

the script that is not working is

function Items.Crate_Award(Tier)
	for Items,Item in pairs(Items.data) do --// Looping thru module	
		if Item.Tier == tostring(Tier) then --// Checking if Item is a table and if Tier_Parameter == Tier
			print(Item[math.random(1,#Items)])
			return Item
		end 
	end
end

The crate hase been set to get always Commen

Why are you checking the tier if it’s random?
And, Tier is most likely already a string if you pass it as one (I assume you do), so you don’t need that part.

There doesnt seem to be an error in the function, could you send the function that creates the Tier.

it finds the item whit tier Commen and i want to choose a random item whit tier index

If you only want to award them a certain tier, you can’t make it random.
What you can do is create a temporary table and store the current tiers (based on their tier), and chose from that.

i dont want want to reward only the tier i want to get the whole index

1 Like

That still doesn’t make sense, because it’ll always find the tier you specify. Hence, it doesn’t make sense why you are checking the tier.

EDIT - I now see what you mean, after some investigation.
I managed to edit your code sligthly as an example (working):

function Items:Award(Tier)
	for Index, Value in pairs(Items) do
		if Value.Tier == Tier then
			local Selected = Items[Random.new():NextInteger(1, #Items)]
			print(('%s has been selected!'):format(Selected.Name))
			return -- Stop the code, otherwise it'll select every tier (based on the tier they have)
		end
	end
end
Items:Award('Commen') -- Award them the tier you want to chose from.

yeahh selected is just nil i am typing extra stuff to send this

1 Like

My example works fine, anything else you need?
You can easily implement / change it if you’d like.

well didnt work for me sadly i tried hard to make it work but i dont need anything else

Doesn’t work? That function works just fine for me.

the selected just prints nil and nothing else :frowning:

Send me your current code and I’ll look into it if you want.

local Items = {}

Items.data = {
	{
		Name = "Hat";
		ImageID = 1409499909;
		Cost = 100;
		Value = 10;
		Tier = "Commen";
		ItemID = 1
	};
	{
		Name = "Pants";
		ImageID = 128302713;
		Cost = 30;
		Value = 100;
		Tier = "Commen";
		ItemID = 2
	};
	{
		Name = "Shirt";
		ImageID = 495262405;
		Cost = 330;
		Value = 1000;
		Tier = "Uncommen";
		ItemID = 3
	};
	{
		Name = "Glasses";
		ImageID = 1439396148;
		Cost = 2000;
		Value = 1000;
		Tier = "Rare";
		ItemID = 4
	};
	{	
		Name = "Chain";
		ImageID = 1027719587;
		Cost = 1233;
		Value = 4623;
		Tier = "Specail";
		ItemID = 5
	};
}

function Items.Get_Items_FromID(ID)
	for Items,Item in pairs(Items.data) do --// Looping thru module	
		if type(Item)== "table" and Item.ItemID == tonumber(ID) then --// Checking if Item is a table and if ID_Parameter == ID
			return Item --// Returns Item if found
		end 
	end
end

--[[function Items.Crate_Award(Tier)
	for Items,Item in pairs(Items.data) do --// Looping thru module	
		if Item.Tier == tostring(Tier) then --// Checking if Item is a table and if Tier_Parameter == Tier
			print(Item[math.random(1,#Items)])
			return Item
		end 
	end
end]]--

function Items.Crate_Award(Tier)
	for Index, Value in pairs(Items.data) do
		if Value.Tier == Tier then
			local Selected = Items[Random.new():NextInteger(1, #Items)]
			print(Selected)
			--print(('%s has been selected!'):format(Selected.Name))
			return
		end
	end
end

return Items

You need to replace Items with Items.data.