How to have multiple pets with same name in table?

I want to have all pets that have same name in the table DataObject[2], but my script only saves one of the pets. I look on other similar posts and they’re recommending using GenerateGUID but I don’t know how to implement that.
Example of the script only saving one of the pets that has same name:

 [2] =  ▼  {
                       [NeonBatDragon] = 1,
                       [NeonBatDragon] = 2,
                       [BatDragon] = 3
                    }

I want the script have the data in the table formatted as this:

 [2] =  ▼  {
                       [NeonBatDragon] = 1,
                       [NeonBatDragon] = 2,
                       [BatDragon] = 3
                    }

Script:

local DataObject = {player.leaderstats.Cash.Value, {}}
	for _,Pet in pairs(player.Pets:GetChildren()) do
		DataObject[2][Pet.Name] = Pet.Value
	end

How can I have multiple pets with same name in DataObject[2] while keeping it the format I want?

1 Like

why not to do Pet.Value = Pet.Name

you cannot have the same keys in the dictionary but Pet.Value is not the same in every pet, right ?

Note : *Do not forget to use tostring(Pet.Value)

No programming languages (none that I know of) allows adding multiple identical indices. If you have “Pet1” index in a table, and you wish to add another value with the same name, you don’t get two “Pet1”'s, you overwrite the previous value of “Pet1”.

local t = {Pet1 = 1} -- expected

print(t.Pet1) -- 1

local t2 ={Pet1 = 1, Pet1 = 2} -- second Pet1 overwrites first Pet1.

print(t2.Pet1) -- 2

Not to mention, you can’t access two “Pet1”'s if using the same index for two values is even possible (save for pairs, but that lowers performance a bit, and that would be too much work to differentiate two same indices).

Would it be better for you to use the following structure?

{
	[1] = {
		Name = "Pet",
		Value = 1234567890
	},
}

Instead of creating a new index and setting the name of it to be the pets name, set it to the current index in the table + 1, and set the value to the amount of pets you have.