Using one datastore to save multiple things of the same name

I’m currently making a pet system, and the inventory folder is turned into a table and then saved. However, I’ve noticed that it doesn’t save multiple things of the same name.

For example:

  • Gets 2 bats (which doesn’t save)

But this saves:

  • Gets 1 dragon & 1 bat (which saves as they are different)

So, is this even possible? If not, how can I work around it? And if it is possible, where am I going wrong? I can provide a script if it’s actually possible, but for now, I’m fairly sceptical on whether it is.

2 Likes

There was a post similar to this just yesterday. My solution was saving pets with a unique ID rather than by name.

Try reading through this post and see if that helps. If not let me know and I can try to work something else out.

1 Like

If you have each stat saved as a ‘slot’ (such as Slot1, Slot2, Slot3 etc etc), you can just make it’s value the name of the pet. I don’t see why that wouldn’t work.

1 Like

You can use ordered datastore and save the array including pet type

1 Like

@mistr88 Can you explain how that would work in more detail?

@K_reex That sounds simple to do. What would be the easiest way of doing this?

--// Saving
PetInventory[v.Name] = v.Name
PetInventory[v.ID] = v:WaitForChild("ID").Value

--// Loading
for i,v in pairs(PetData) do
print(v)
local Target = Pets:FindFirstChild(v)
if Target then
local Clone = Target:Clone()
Clone.Parent = PetInventory
				
local PetID = Instance.new("IntValue")
PetID.Name = "ID"
PetID.Parent = Clone
					
PetID.Value = v.ID
end
end

^ Snippets only, this is not the whole script, just relevant parts to this question

So, what I’m thinking is:

  1. Pet is bought, added to player’s “pet inventory” folder in player’s children
  2. IntValue in pet’s children is set to player’s OwnedPet’s value (unique ID)
  3. OwnedPet’s value increases by 1 for the next pet
  4. When player leaves, ID in table is assigned like this:
    PetInventory[v.ID] = v:WaitForChild("ID").Value
  5. When player joins, get the ID from the table where the name is, and assign it to the pet’s IntValue’s value.

Would this process work, and would the script that I showed at the top ^^ work?

I believe that would work. Try implementing it and report the results! The important thing is that you’re saving / loading the pet by unique ID, and not the name.

1 Like

lol, i badly read it, and ovelooked, that it is in table, so for this, just use not asociative tables

Just tried it. From what I tested, it didn’t load any of the pets.

Can you give an example and explain how you’d load a pet by its unique ID? Pet systems are fairly new to me, but I’m trying to grasp it. :slight_smile:

If you are doing that, just use in values, because it indicates how much of the same thing you have

I’m not entirely sure how your specific pet system works, but:

In the case that the pet is an instance with the following values instances within:
string Pet_Type … Type of pet (“Dog”, “Cat”, “Bunny”) – this should be prebuilt into the pet model
int Pet_ID … Unique ID of the pet
string Given_Name … Whatever the player named their pet (“Bobby”, “Duke”, “Vladmir”)

We want to store the pet with Pet_ID, not Pet_Type or Given_Name because those can be the same for multiple pets.

-- In a server script

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService"):GetDataStore("pet_datastore")
local PetStorage = game.ServerStorage.PetStorage -- where all the pet models are kept

-- Saving
Players.PlayerRemoving:Connect(function(Player)
    local Inventory = Player.Pet_Inventory
    PetData = {}

    for _, PetObject in pairs(Inventory:GetChildren()) do
        PetData[PetObject.Pet_ID.Value] = {
            Pet_Type = PetObject.Pet_Type.Value,
            Given_Name = PetObject.Given_Name.Value
        }
    end

    DataStore:SetAsync(Player.UserId, PetData)
end)

-- Loading
Players.PlayerAdded:Connect(function(Player)
    local Inventory = Instance.new("Folder")
    Inventory.Name = "Pet_Inventory"
    Inventory.Parent = Player

    local PetData = DataStore:GetAsync(Player.UserId)

    if PetData then
        for i,v in pairs(PetData) do
            local PetObject = PetStorage[v.Pet_Type]:Clone()
            PetObject.Pet_ID.Value = i
            PetObject.Given_Name.Value = v.Given_Name
        end
    else -- Player has never played the game before, and doesn't have any pet data
        DataStore:SetAsync(Player.UserId, {})
    end
end)
3 Likes

I didn’t realise it was that simple, I was overthinking it. Thanks, it helped a lot! :slight_smile:

1 Like