Is there a way to have dictionary duplicate key but different value?

Hello, just keep in mind im new to OOP so no judge my code haha.

HOW MY SCRIPT WORKS
How my scripts works is that everytime a player join, it creates an player object and it contains the playerdata and the functions we have for the player we can call like PlayerObject:GiveItem(id)

so basically the player data usually goes like this

['Inventory'] = {
  ['1'] = {
    ['quantity'] = 15,
    ['object'] = nil,
  },
  ['2'] = {
    ['quantity'] = 15,
    ['object'] = nil,
  }
}
}

So basically the number represent the ItemID (its string because i learn that you should make data type string instead of int if you’re not doing math with it), and it have quantity and item object, which we can assign to so we can use the item function like ItemObject:Use() by calling it in player inventory like

PlayerObject.data['Inventory'][tostring(itemid)]['object']:Use()

MAIN PROBLEM
So i wanted to have a system like in minecraft dungeons where item are the same but they have different level and stats, for example, We have rare sword, dropped by a monster, and the sword is not stackable (i still dont know how to make item unstackable from my skript) and the sword that is dropped have different damage but is still the same item, for example a sword that have damage range of 500-600 when drop, it still the same item, same item id but different because of their stats

but because they have same id, (which is the key we do for player inventory) , but different value, it simply not possible to make it like this

['Inventory'] = {
  ['15'] = { --sword id
    ['quantity'] = 1;
    ['object'] = itemobject,--this where we store the item stats like damage
  },
  ['15'] = { -- sword id
    ['quantity'] = 1;
    ['object'] = itemobject, --this where we store the item stats like damage
  }
}

because well duplicate key are simply not possible

SOLUTION I TRIED
I dont know how to make generate a number that wont EVER duplicate so that every item differs from each other and my other skript already relies on the item id being the key value of the player inventory, i could do

['Inventory'] = {
  ['1:1'] = { --[itemid:ownkey], seperate the itemid and its own key
    ['quantity'] = 1,
    ['object'] = nil,
  },
  ['1:2'] = { --[itemid:ownkey], now this have same id but different key
    ['quantity'] = 1,
    ['object'] = nil,
  }
}

i could do tick() for its own key seperated by item id but what if 2 same item drop at the same time?

Any help, suggestion and guidance is appreciated.
Thanks you so much for your time.

I think the solution of ownkeys would work just fine. Can you share more info on why you haven’t used it and how your player and object systems are structured?

You could then do something like this to get items of the same class:

function PlayerObject:GetAllItemsWithName(name: string): (any?)
    local foundItems = {}

    for item, info in next, self.Inventory, nil do
        local rawName = string.split(item, ":")[1]
        if rawName == name then
            table.insert(foundItems, item)
        end
    end

    return table.unpack(foundItems)
end
1 Like

Hello, thanks for your time and for the suggestion. as to your questions.

It would work by i have no idea how to generate a unique key for every item so there is no duplicate, as said using tick() might be bad, i could also make unique key 1 , 2 or 3 based on how many item of that id the player has, but doing that might cause a problem if theres like, trading system, where player have their own itemid with the same unique key as everyone else.

but the script you provided is really helpful, it helped solved it a step closer, thanks!

You’d probably want to use a different ID system for trading and keep the item numbers just for the inventory. It’d be a pain otherwise. I’d say keep this number specifically for the player’s inventory.
Here’s a way you could do something like this:

--example where item has a property "Name"
function PlayerObject:AddToolNumberIndex(item: any?): nil
    local itemsWithName = self:GetAllItemsWithName(item.Name)
    item.Name ..= #itemsWithName + 1
end
1 Like

Oh that is smart, thats a way to solve what im having problem with

Thanks so much for your time and help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.