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
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.
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.
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.
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