How would I store multiple of the same item in a dictionary?

I’m working on a game which will allow the player to buy pets. I’m storing the player’s pets as a dictionary in Datastores. An example dictionary would look like this:

{["Dog"] = 1, ["Cat"] = 0} -- 1 means equipped, 0 means stored in inv

The only issue I’m facing is how I should store multiple of the same pet. If the player has two dogs, then how would I be able to differentiate between the two when the player tries to equip one? Dictionary.Dog only returns the latest item in the dictionary.

1 Like

Why not have an array of modes for different pets as the value?
For example:

{
    Dog = {1, 0};
}

This would mean that Dog1 is Equipped, Dog2 however is not. Of course, you could use dictionaries instead of just modes.

2 Likes

While both items are dogs, they are not the same item. I would recommend giving each dog a different name and storing them like so:

Inventory = {
  BrownDog = 0,
  SpottyDog = 1,
  PlainCat = 0,
}

As for which one is equipped, personally I’d do something like:

Equipped = {
  Pet = "SpottyDog"
}

If you go with that method, players can then own more than one of the same item. The number in their inventory represents how many of that item they have.

3 Likes

Thats interesting, and for each new pet would I just insert a 1 or a 0 into the table?

1 Like

How would I give each dog a new name though? The player could have up to 100 pets and they could all be the same dog, would I have to generate a new name for each dog in their inventory?

In which case, I would use an array as suggested above.

Pets = {
    {
        Type = 1, -- dog
        Equipped = false,
        -- additional pet info
    }
    {
        Type = 1, -- dog
        Equipped = true,
        -- additional pet info
    }
    {
        Type = 2, -- cat
        Equipped = false,
        -- additional pet info
    }
}
1 Like

This seems to be the easiest way, thanks!