I’m currently in the process of saving my pet system. Each pet has a UUID that needs to be saved. When I’m using SetAsync, I’m putting all of the UUIDS inside a table. And when I’m using GetAsync, it prints out the error in the title. I know why it happens, but I have no idea how to fix it.
game.Players.PlayerAdded:Connect(function(player)
local inventory = PetDataStore:GetAsync(player.UserId.."pet")
local uuid = PetDataStore:GetAsync(player.UserId.."uuid")
if inventory and uuid then
for i, petName in pairs(inventory) do
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
local stringValue = Instance.new("StringValue")
stringValue.Name = petName
stringValue.Parent = player.PetInventory
stringValue.Value = uuid
end
end
end
end)
local function saveInventory(player)
if player:FindFirstChild("PetInventory") then
local inventory = {}
local uuid = {}
for i, pet in pairs(player.PetInventory:GetChildren()) do
table.insert(inventory,pet.Name)
table.insert(uuid,pet.Value)
end
local success, errorMessage = pcall(function()
PetDataStore:SetAsync(player.UserId.."pet",inventory)
PetDataStore:SetAsync(player.UserId.."uuid",uuid)
end)
if success then
print("Data saved")
else
print("Error: "..errorMessage)
end
end
end
game.Players.PlayerRemoving:Connect(function(player)
saveInventory(player)
end)
Although I have to recommend using one dictionary for all player data instead of two separate tables. Instead of having one Key for level, one for pet names, one for UUIDs, you should just use a single one, that maybe looks like this(it’s just an example but you get the idea):
local Data = {
Level = 12,
Experience = 5032,
Pets = {
["684C1C61-F5CC-449A-8173-7310C47E200C"] = { Name = "dog", Level = 3 },
["CC253695-A160-44C5-9F76-AE81BB91743A"] = { Name = "cat", Level = 12 }
}
}
This way allows you to keep the data more compact and avoid unnecessary DataStore calls.
It would work but only with a couple of issues. What if you have more of the same pet? I personally think that would create problems with the scripts even if I changed them. And with trading the script would somehow have to determine if a pet has been duped or not by checking if 2 of the same uuids exist.
I don’t see why it would be any worse than the system you already have? You keep the UUID and the data together, instead of in separate tables, relying on the indices. The keys of the dictionary are the UUIDs, so you could keep virtually infinite amounts. When performing a trade, you can easily set the data on the receiving player to the pet data, and then remove it from the player sending the pet.