How do I change the table's contents?

How do I make this print:

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

Instead of this:

                       [1] = "NeonBatDragon",
                       [2] = "NeonBatDragon",
                       [3] = "BatDragon"
                    }

Script:

    local DataObject = {player.leaderstats.Cash.Value, {}}
    for _,Pet in pairs(player.Pets:GetChildren()) do
        table.insert(DataObject[2], Pet.Name)
    end
    print(DataObject)
1 Like
for index: number, Pet: Instance in pairs(player.Pets:GetChildren()) do
    DataObject[2][Pet.Name] = index
end
2 Likes

ty but it only prints:

[2] =  ▼  {
                       ["NeonBatDragon"] = 3

I want it to print all the pets, not just the last one

1 Like
print(DataObject[2])

Try this?

1 Like

I tried that already. How do I make it print all the pets?

1 Like

You have duplicate pets with the same name, so it overwrites their data, and FYI you can’t have the same keys in a dictionary.

1 Like

Is there a way to fix that? or the only way to fix it is to change the name of the pets

1 Like

Yes, you have to change the pet names or you have to use arrays. If you really want duplicate pets you have to use arrays.

1 Like

How do I fix it using arrays? I am not familiar with arrays

1 Like
for index: number, Pet:Instance in pairs(player.Pets:GetChildren()) do
    table.insert(DataObject[2], {Name = Pet.Name, Index = index})
end

Then just,

print(DataObject[2]) -- {{Name = ..., Index = 1}, {Name = ..., Index = 2}, ...}

Does this work @CAAplayer

3 Likes