How to save someone's pet inventory?

  1. What do you want to achieve? Save someone’s pet inventory

  2. What is the issue? I want to learn if that would work or if that’s true.

Would the following script work?

local datastoreservice = game:GetService("DataStoreService")
local data = datastoreservice:GetDataStore("petinventory")
game.Players.PlayerRemoving:Connect(function(player)
local petfolder = player.PetFolder
for _,pets in pairs(petfolder:GetChildren()) do
data:UpdateAsync(pets.Name,pets.Amount.Value)
end
end)

This is not a proper way to do it.
What I would do is create a dictionary with the player’s pets and their values, and save that to the datastore.

The main idea is to input all the data into a table and save that. Instead of saving the actual instance, in the inventory, save the data about the object such as the name and anything else you may need. The name is very important since when we load, we will need it to identify the pet being loaded back. Here is what a basic save function could look like (it wont work and is not very good but it gets the idea across):

local save = {}
for i, v in pairs(inventory) do
     table.insert(save, {v.Name, v.Amount.Value})
end

-- Save the table

Then when you want to load it back in again, you’d simply loop over the saved table and do the inverse operation which could look something like this:

local data = data:GetAsync(plr.UserID)

for i, v in pairs(data) do
    local pet = pets:FindFirstChild(v[1])
    
    if pet then
        local newPet = pet:Clone()
        newPet.Parent = LOCATION
        newPet.Amount.Value = v[2]
    end
end

If you want to see a more complex and working example of something similar to this, you can see my open source saving models place. It’s a bit different than what you’re doing, however it uses this exact same concept. Hope this helped!

Thank you, I will try this and mark as solution if it helps me. :slight_smile: