Adding a new dictionary to a table without overwriting other one of same name

Heya,

Currently working on a datastore system saving a table with items and some of their custom properties for my RPG game. The system worked fine to the point where stored items of the same name don’t get saved, as it overwrites and old value incase it exists.

Ideas for an easy way to prevent this overwriting?

Code Sample :

function data:AddItem(plr, item, itemtype)
	if itemtype == "Vehicle" then
		data[plr].Items[item.Name] = {Damage = item.Info.Damage.Value, Fuel = item.Info.Fuel.Value} -- The point where it overwrites this old dictionary if one exists with the same name.
		self.Character.Humanoid:UnequipTools()
		wait(.2)
		item:Destroy()

So, you’re trying to not overwrite it correct? And have 2 items with the same name?

Because you can’t have a table with the same index twice, it will just overwrite the previous index. That’s just how tables work.

1 Like

Like @MrLonely1221 said, tables cannot have the same index it’ll just overwrite the old one to the new one’s value.

Seeing from the script you have attached what I suggest is for each itemtype you create a new table inside the Items table.

so your data table would look like this :

local DataTable = {
   Items = {
       Vehicles = {},
       OtherItemType = {},
       OtherItemType2 = {}
   }
}

With your current script you’d do like this :

function data:AddItem(plr, item, itemtype)
	if itemtype == "Vehicle" then
		data[plr].Items.Vehicles[item.Name] = {Damage = item.Info.Damage.Value, Fuel = item.Info.Fuel.Value} -- The point where it overwrites this old dictionary if one exists with the same name.
		self.Character.Humanoid:UnequipTools()
		wait(.2)
		item:Destroy()

Unless you have vehicles with the same name, in that case you’d generate an ID for each vehicle rather than relying on the name to save it.

1 Like

HttpService:GenerateGUID() can be used to create randomly generated universally unique identifier (UUID) string.

You could use this to identify your items if you wanted and then store that in a table so you can access it easily, or you can make your own UUID such as ItemName-ItemType etc.

1 Like

Instead of using an ID, couldn’t you - just like you’re doing for the data dictionary - have the keys as the individual Instances? If the Name is needed then it could just be stored in the value’s Dictionary.

Here’s an example:

data[plr].Items[item] = {
    Damage =  item.Info.Damage.Value;
    Fuel = item.Info.Fuel.Value;
    Name = item.Name;
};

As @ReturnedTrue says you could use the instance as the key.

The simple fact is that you cannot have more than one key which is the same.

You haven’t given us much context but you could maybe have a table for each player assuming that each player can only ever have one of each item.

Or create a random key that you use for each and add the key as a value to the item.

We’d need more info to help you more specifically.

function data:AddItem(plr, item, itemtype)
	if type == "Vehicle" then
		data[plr].Items[tostring(HttpService:GenerateGUID(false))] = {ItemName = item.Name, ["Damage"] = item.Info.Damage.Value, ["Fuel"] = item.Info.Fuel.Value}
		plr.Character.Humanoid:UnequipTools()
		wait(.2)
		item:Destroy()

Thanks, this way seems pretty reliable to me.

What people are saying above is true, you could use the instance itself as the key, but if you’re trying to save data it wouldn’t work.

The GUID should always work for you without troubles.

1 Like