Best way to store items of the same item ID

Bit of an open-ended question here.

Say I have an item ID module where each item I make is added and assigned an ID. One of these items could be “Iron sword” and I want each copy of the iron sword to have its own data, for example, having a Level 1 Iron sword, and a second Level 5 Iron sword, while sharing the same item ID with every other iron sword.

What’s the best way to go about doing this?

I have come up with two solutions, but would prefer if there was a better way:

  1. Make a new item ID for every level of the item

  2. Make the data for the item ID for each weapon equal to a table. Fill the table with a new “sub ID” for every copy of the weapon you own. However this makes the weapon hard to reference because it exists in the datastore rather than a module.

Not sure If I understood you correctly, correct me if that’s the case.

You can differentiate each item by it’s level, unless you have more than one item that it’s the same level, if that’s the case, just increase a quantity for the same item, like

local Inventory = {
    ["Iron Sword"] = {
        Level = 1,
        Id = 213123, 
        Quantity = 3
    },
    ["Iron Sword"] = {
        Level = 5,
        Id = 213123, 
        Quantity = 1
    }
}

local sword1
local sword2

for _, item in pairs(Inventory) do
   if item.Level >= 5 then
       sword2 = item
   else
       sword1 = item
   end
end

print("Iron Sword Lvl 1", sword1)
print("Iron Sword Lvl 5", sword2)

Well the way I’ve done it is quite similar. Let’s say the id for the weapon is 14, and in my inventory datastore it looks something like

Items = {
     [14] = {}
}

Then when you pick up an item it adds it to to the datastore

local wepcount = (data.Items[ID])
                     local wepstats = {
                            Rank = 0,
                            Level = 0,
                            Color = 0,
                            Experience = 0,
                    }
table.insert(data.Items[ID], #wepcount+1, wepstats)

But we run into the same problem, it’s stored in the datastore and not fixed in a module, making it hard to reference a specific sub-item of this ID, especially when it is so dynamic and can be removed/added/changed at any time, not to mention anything funky that happens with the datastore itself because of the method used to store it. I am hoping to find a way that is both easy to reference and highly variable.

1 Like

Maybe using OOP (Object Oriented Programming) would work better in this situation, have you tried it before?

1 Like

I am familiar with the concept but am having trouble seeing the practical usage in this particular scenario. Could you provide an example?

Can you store a property multiplier value for each lvl?

like, if you need an Iron Sword lvl any,
get:

IronSword = { -- or "213123" (id) instead of item's name
    damage = basedamage, damageMultiplier = 1.25,
    atkSpeed = baseAtkSpeed, speedMultiplier = 1.3,
    ID = 00000,
    ...
}

IronSwordlvl1 = IronSword:Clone()

IronSwordlvl2 = IronSword:Clone()
IronSwordlvl2.damage *= IronSwordlvl3.damageMultiplier 
IronSwordlvl2.atkSpeed *= IronSwordlvl3.speedMultiplier

IronSwordlvl3 = IronSword:Clone()
IronSwordlvl3.damage *= IronSwordlvl3.damageMultiplier * 2
IronSwordlvl3.atkSpeed *= IronSwordlvl3.speedMultiplier * 2

IronSwordlvl4 = IronSword:Clone()
IronSwordlvl4.damage *= IronSwordlvl3.damageMultiplier * 3
IronSwordlvl4.atkSpeed *= IronSwordlvl3.speedMultiplier * 3

IronSwordlvl5 = IronSword:Clone()
IronSwordlvl5.damage *= IronSwordlvl3.damageMultiplier * 4
IronSwordlvl5.atkSpeed *= IronSwordlvl3.speedMultiplier * 4

(you can also automatize this proccess of multiplication)

local function getItem(Name, Lvl) -- or ID instead of name
    -- get base item wherever it is from
    local item = ItensToolsFolder[Name]:Cone()

     -- change its properties acording to the lvl and multiplier
    item.AtkSpeed.Value = ItensTable[Name][atkSpeed] + (ItensTable[Name][atkSpeed] * (ItensTable[Name][atkSpeedMultiplier] * (Lvl - 1) ))

    return item
end

Is this Id inside of a table(Iron Sword)? So basically like this

local myDictionary = {
  ["Iron Sword"] = {
    Id = "1434"
  }
}

Like that?

The way I would do it is just like this

local myTable = {
["Iron Sword"] = {
    [1] = {
        name = "blablab"
        Id = "balbabla"
        type = "lalala"
    },
 [2] = {
        name = "blablab"
        Id = "balbabla"
        type = "lalala"
    }
  }
}

that’s how I do it.
Also you need to store the index into itself to know what item you are going to get.

pls feedback what you think about :smiley:

Just to clarify I am not asking how to accomplish the methods I have already listed, as that is the easy part. I am looking to find a better way of doing things - either by making it more variable in the case of a different ID for each level, or by making it more easily accessible in the case of instantiated IDs in a table OR a new way entirely.