# Having multiple items with same name in table?

I want to save a players pets, and thus if a player has 3 Beagles for example, I can’t go saving them each as Beagle, Beagle, Beagle. So how can I work around this problem? I also don’t wanna save as an index, as then if they got rid of say ‘[2]’ you’d be left with 1 and 3

4 Likes

You could index them with a combination of name and an ID, like “00001_Beagle.” That way, they would still have the name in entry.

That being said, if the pets have any degree of complexity to their data, it would be in your best interest to concoct some sort of ID-ing system and saving each pet as its own table of data with the pet’s name being an entry in that table.

Well I was gonna save like so

``````["Pets"] = {
["Beagle"] = {Age = 0, Shiny = false, Needs = {Hunger = 50, Happiness = 50}},
["Beagle"] = {Age = 2, Shiny = false, Needs = {Hunger = 20, Happiness = 55}},
["Beagle"] = {Age = 0, Shiny = true, Needs = {Hunger = 2, Happiness = 87}},
}
``````

And the index of each table would be the name the player gave their pet. But if the player doesnt name the pet, itd just be the name of the pet breed.

Pretty sure this tho would result in only one of the 3 items being saved? (I believe tables cant have the same index used)

Usually I do it like this:

``````local Pets = {Beagle = 3, Cats = 2, Proxus = 7}

if Pets[PetName] then
Pets[PetName] += Amount
else
Pets[PetName] = Amount
end
end

function GetPet(PetName)
if Pets[PetName] then
return Pets[PetName]
else
return 0
end
end
``````
1 Like

You are correct in that assessment. Because of that issue, the only solution is to add more data to your entry, but that would mean that you can’t use the name as the identifier.

While it is faster to look up entries with a key, unless your accessing the table extremely frequently, iterating through an indexed table isn’t that much slower. Moreover, `table.remove()` would resolve your “1, 2, 3; take away 2 and be left with 1, 3” situation because the function shuffles everything down to fill in the gap. Because a pet’s name is not a unique identifier, you will have to actually evaluate the data of each pet when trying to determine identity.

1 Like

The other problem I forgot to mention tho is all data gets converted to phyiscal folders and values. So to remove a piece of data, im destroying the folder (not using table.remove)

This is so the client has quick/easy access to view data changes, without having to mess with client-server-client communication constantly. So deleting folder 2, would still result in folder 1 and 3 being left

Why don’t you generate a guid to index it and put the name of it into the table with all the rest?

What’s a guid???

It generates a 32 hexadecimal. In this case you would have to shorten the length, but using this will allow you to have a unique identity to each Beagle.

2 Likes

I feel that’s a lil too advanced for what I’m going for

It’s just a unique identifier for each item. It’s not that complex.

``````["Pets"] = {
{Name = "Beagle", GUID = ..., Age = 0, Shiny = false, Needs = {Hunger = 50, Happiness = 50}},
{Name = "Beagle", GUID = ..., Age = 2, Shiny = false, Needs = {Hunger = 20, Happiness = 55}},
{Name = "Beagle", GUID = ..., Age = 0, Shiny = true, Needs = {Hunger = 2, Happiness = 87}},
}
``````

You can tell the difference between any pet by looking at their GUID.

1 Like

Ye, but I’m currently using a backpack system,

``````Backpack = {
["Tools"] = {["Basic Rod"] = {}},
["Pets"] = {},
["Vehicles"] = {},
["Foods"] = {},
["Toys"] = {},
},
``````

I don’t particulraly want everything else in the backpack to be stored with dictionary/arrays and then pets to be stored using some long 32 character number

Then make it shorter than 32 characters. You can use `string.sub` to get a substring. However, this can cause collision issues. You can also try using the time they acquired the asset as a GUID. (Unless there is a chance for players to obtain the same item at the exact same second)

I mean, I don’t want ANYTHING being stored with random characters. I want clean and concise naming. Not jumbled characters, ids, whatever.