Getting all items for a list and putting them in a table

Basically, I have my items below in a module

local classItems = {}

classItems.Weapons = {
	Knight = {
		['Sword'] 			= {Cost = 0, Currency = '', Rarity = 'Common'},
		['Claymore'] 		= {Cost = 100, Currency = 'Gold', Rarity = 'Common'},
		['Highlander'] 		= {Cost = 200, Currency = 'Gold', Rarity = 'Common'},
		['Light'] 			= {Cost = 250, Currency = 'Gold', Rarity = 'Rare'},
		['Long Sword'] 		= {Cost = 500, Currency = 'Gold', Rarity = 'Rare'},
		['Trusty'] 			= {Cost = 50, Currency = 'Gems', Rarity = 'Epic'},
		['Riptide'] 		= {Cost = 100, Currency = 'Gems', Rarity = 'Epic'},
		['Shai'] 			= {Cost = 200, Currency = 'Gems', Rarity = 'Legendary'},
		['Judgement'] 		= {Cost = 250, Currency = 'Gems', Rarity = 'Legendary'},
	},
	
	Archer = {
		['Bow'] 			= {Cost = 0, Currency = '', Rarity = 'Common'},
		['Stringer'] 		= {Cost = 50, Currency = 'Gold', Rarity = 'Common'},
		['Steel'] 			= {Cost = 100, Currency = 'Gold', Rarity = 'Common'},
		['Imperial'] 		= {Cost = 150, Currency = 'Gold', Rarity = 'Common'},
		['Hawkeye']			= {Cost = 250, Currency = 'Gold', Rarity = 'Rare'},
		['Darkage'] 		= {Cost = 500, Currency = 'Gold', Rarity = 'Rare'},
		['Splinter'] 		= {Cost = 25, Currency = 'Gems', Rarity = 'Epic'},
		['Daedric'] 		= {Cost = 50, Currency = 'Gems', Rarity = 'Epic'},
		['Ornate'] 			= {Cost = 100, Currency = 'Gems', Rarity = 'Legendary'},
	},
}

classItems.Armours = {
	Knight = {
		['Griswold'] 		= {Cost = 0, Currency = '', Rarity = 'Common'},
		['Redcliff'] 		= {Cost = 100, Currency = 'Gold', Rarity = 'Common'},
		['Jahrfyre'] 		= {Cost = 150, Currency = 'Gold', Rarity = 'Rare'},
		['Enchanted'] 		= {Cost = 75, Currency = 'Gems', Rarity = 'Rare'},
		['Empyrean'] 		= {Cost = 150, Currency = 'Gems', Rarity = 'Epic'},
	},
	
	Archer = {
		['Iron'] 			= {Cost = 0, Currency = '', Rarity = 'Common'},
		['Ace'] 			= {Cost = 100, Currency = 'Gold', Rarity = 'Common'},
		['Daring'] 			= {Cost = 150, Currency = 'Gold', Rarity = 'Rare'},
		['Adventurous'] 	= {Cost = 75, Currency = 'Gems', Rarity = 'Rare'},
		['Alivia'] 			= {Cost = 150, Currency = 'Gems', Rarity = 'Epic'},
	},
}

return classItems

Now I want to get each of those individual items and chuck them in a table

local AllItems = {}

-- Get the random item
for i, v in pairs(ClassItems) do
	print(i, v) -- Would Print Weapons table: 0000013BDD252330 / Armours table: 0000013BDD2542C0
end

Doing table.insert(AllItems, v) would just insert the Knight and it’s table, instead of just all the items inside the Knight table. Please note, I cannot change how the item table is sorted/arranged as it is this way for other reasons when referencing it from other scripts.

What I am trying to accomplish eventually is for one of the items be selected. It will be based on percentages too, which I plan on doing as so:

Say
Common = 50%
Rare = 25%
Epic = 15%
Legendary = 10%

Then I’d insert all the common items into the table 50 times, all the rare items into the table 25 times, so on for the rest. This way it’s not just a completely random pull and the item it would select would be based off this percentage

3 Likes

You aren’t asking a question anywhere in your post. What part do you need help with?

2 Likes

Getting all the items from my list and putting them into a sinlge table appropriately but I need to still be able to see each items rarity, as I need to use that to include percentages when picking a random item

2 Likes

Don’t you mean you just want to loop through twice? I.e loop through the classItems, and then through each item inside that.

local AllItems = {}

-- Get the random item
for pos, weaponClass in pairs(ClassItems) do
	for weaponName, weaponValues in pairs(weaponClass) do
        print(weaponName) -- the weapon name
        print(weaponValues) -- the table of stats
        print(weaponValues.Rarity) -- it's rarity
        -- add these to table however you wish
    end
end
3 Likes

That doesn’t work, as weaponName is just the class name

for pos, weaponClass in pairs(ClassItems) do
	for weaponName, weaponValues in pairs(weaponClass) do
        print(weaponName)
        print(weaponValues)
        print(weaponValues.Rarity)
    end
end

I’ll just shorten the class list for examples sake

classItems.Weapons = {
	Knight = {
		['Sword'] 			= {Cost = 0, Currency = '', Rarity = 'Common'},
		['Claymore'] 		= {Cost = 100, Currency = 'Gold', Rarity = 'Common'},
		['Highlander'] 		= {Cost = 200, Currency = 'Gold', Rarity = 'Common'},
		['Light'] 			= {Cost = 250, Currency = 'Gold', Rarity = 'Rare'},
		['Long Sword'] 		= {Cost = 500, Currency = 'Gold', Rarity = 'Rare'},
		['Trusty'] 			= {Cost = 50, Currency = 'Gems', Rarity = 'Epic'},
		['Riptide'] 		= {Cost = 100, Currency = 'Gems', Rarity = 'Epic'},
		['Shai'] 			= {Cost = 200, Currency = 'Gems', Rarity = 'Legendary'},
		['Judgement'] 		= {Cost = 250, Currency = 'Gems', Rarity = 'Legendary'},
	},
}

It will print ‘Knight’ then a table, then nil.

1 Like

Couldn’t you do this?

for pos, weaponClass in pairs(ClassItems) do
	for weaponName, weaponValues in pairs(ClassItems[pos]) do
        print(weaponName)
        print(weaponValues)
        print(weaponValues.Rarity)
    end
end
1 Like

Ah, I was one too many loops short. In that case, just loop through WeaponValues and that will give you each individual weapon and it’s respective data.

for pos, weaponClass in pairs(ClassItems) do
	for weaponSubClass, weaponTable in pairs(weaponClass) do
        for weaponName, weaponData in pairs(weaponTable) do
            print(weaponName)
            print(weaponData.Rarity)
        end
    end
end

cc @tlr22
Here’s what I got, and it seems to work

for _, allItems in pairs(ClassItems) do
	for className, class in pairs(allItems) do
		for itemName, item in pairs(class) do
			table.insert(Items, {itemName, item.Rarity})
		end
    end
end

Any clue on how to get percentages into this tho?

Cause just using math.random and picking a random item would just be 1/#Items, which I don’t want.

If I have it set as
Common = 50%
Rare = 30%
Epic = 20%
Legendary = 0%
Then I want the ‘random’ item to be selected using these percentages, instead of just being random

1 Like

Honestly you would be better off putting them in a dictionary thing. So you can just do print(AllItems.Sword.Rarity). You first have to loop through classItems and then you will get weapons and armours in the first loop. The second loop would be like knight, or archer. And the 3rd loop is the actual items and in the third loop we finally put items in the AllItems table.

local AllItems = {}
for _,Type in pairs(ClassItems) do--Type is ClassItems.Weapons and ClassItems.Armours
	for _,Type2 in pairs(Type) do --Type2 would be like Knight or Archer
		for name, value in pairs(Type2) do --name would be like 'Sword' or 'Claymore'. value is the actual dictionary. 
			AllItems[name] = value --Now just put it into the AllItems dictionary.
		end
	end
end

With this you can just do print(AllItems.Sword.Rarity) and boom.

if this list is only for getting a random item of varying rarity,
you can put duplicates of lower quality items so 1/#items is your desired percentage

  • so legendary is 1% you need total number of items to be 100x#legendaries
  • then (% desired*total Items)/(number of that rarity) = copies of each item of that rarity

or you can make lists of specific rarities then have random figure out which rarity was gotten then a second random to figure out which of that rarity was found.

  • first is random 0-1 with ranges conditioned if rand>.99 and rand<=1 then leg
  • second is just random number 1, #items for a specific rarity
1 Like