Only 1 Each Pet Saved

Code:

--// Services 
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local DataStore = game:GetService("DataStoreService")

--// Variables
local CreateStats = require(RS.Modules.Stats)
local GlobalData = DataStore:GetDataStore("TestData")

--// Function

function getMultiplier(Player, multiplierName)
	-- put "Multipler1" or "Multiplier2" for the multiplierName parameter
	local Multi = 0
	for i,v in pairs(Player.Pets:GetChildren()) do
		if v.Equipped.Value == true then
			Multi = Multi + v[multiplierName].Value
		end
	end
	return Multi
end

function getLevel(totalXP)
	local Increment = 0
	local RequiredXP = 100
	for i = 0, RS.Pets.Settings.MaxPetLevel.Value do
		RequiredXP = 100 + (25*i)
		if totalXP >= (100*i) + Increment then
			if i ~= RS.Pets.Settings.MaxPetLevel.Value then
				if totalXP < ((100*i) + Increment) + RequiredXP then
					return i
				end
			else
				return i
			end
		end
		Increment = Increment+(i*25)
	end
end

function LoadStats(player)
	local leaderstats = CreateStats.createObject("Folder","leaderstats",player)
	local Coins = CreateStats.createObject("NumberValue","Coins",leaderstats,1000)
	local Gems = CreateStats.createObject("NumberValue","Gems",leaderstats)
	local Data = CreateStats.createObject("Folder","Data",player)
	local Pets = CreateStats.createObject("Folder","Pets",player)
	local AutoEgg = CreateStats.createObject("BoolValue","AutoEggOwned",Data,false)
	local TripleEgg = CreateStats.createObject("BoolValue","TripleEggOwned",Data,true)
	local MaxEquip = CreateStats.createObject("IntValue","MaxEquip",Data,5)
	local MaxStorage = CreateStats.createObject("IntValue","MaxStorage",Data,30)
	local PlayerWalking = CreateStats.createObject("BoolValue","isWalking",Data,false)
	local Loaded = CreateStats.createObject("BoolValue","Loaded",player,false)

	Loaded.Value = true
end

function GetCoins(player)
	player.leaderstats.Coins.Value += 1 + getMultiplier(player, "Multiplier1")
end

function GetPlayerData(player)
	local Key = player.UserId

	local PlayerData
	local success,warning = pcall(function()
		PlayerData = GlobalData:GetAsync(Key)
	end)
	if success and PlayerData then
		warn("Getting player Data")
		local leaderstats = PlayerData["leaderstats"]
		local PetsData = PlayerData["Pets"]
		local Data = PlayerData["Data"]

		for name,value in pairs(leaderstats) do
			local PlayerStats = player:WaitForChild("leaderstats"):FindFirstChild(name)
			if PlayerStats then PlayerStats.Value = value end
		end
		for name,value in pairs(Data) do
			local PData = player:WaitForChild("Data"):FindFirstChild(name)
			if PData then PData.Value = value end
		end
		if PetsData ~= nil then
			for i,v in pairs(PetsData) do
				local PetObject = RS.Pets.PetFolderTemplate:Clone()
				local Settings = RS.Pets:WaitForChild("Models"):FindFirstChild(v["Name"]).Settings
				local TypeNumber = RS.Pets.CraftingTiers:FindFirstChild(v["Type"]).Value
				local Level = getLevel(v["TotalXP"])
				PetObject.Name = v["Name"]
				PetObject.Equipped.Value = v["Equipped"]
				PetObject.TotalXP.Value = v["TotalXP"]
				PetObject.Multiplier1.Value = Settings.Multiplier1.Value * (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber) + (Settings.LevelIncrement.Value * Level)
				PetObject.Multiplier2.Value = Settings.Multiplier2.Value * (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber) + (Settings.LevelIncrement.Value * Level)
				PetObject.PetID.Value = v["PetID"]
				PetObject.Type.Value = v["Type"]
				PetObject.Parent = player.Pets
			end
		end
	end
end

function SaveData(player)
	local Key = player.UserId

	local PlayerData = {
		leaderstats = {};
		Pets = {};
		Data = {};
	}
	if player.Loaded.Value == true then
		for _,v in pairs(player.leaderstats:GetChildren()) do
			PlayerData["leaderstats"][v.Name] = v.Value
		end
		for _,v in pairs(player.Data:GetChildren()) do
			PlayerData["Data"][v.Name] = v.Value
		end

		for index,value in pairs(player.Pets:GetChildren()) do
			
			PlayerData["Pets"][value.Name] = {
				Name = value.Name,
				TotalXP = value.TotalXP.Value,
				Equipped = value.Equipped.Value,
				PetID = value.PetID.Value,
				Multiplier1 = value.Multiplier1.Value,
				Multiplier2 = value.Multiplier2.Value,
				Type = value.Type.Value
			}
			print(index,value)
			end
		local success,warning = pcall(function() GlobalData:SetAsync(Key,PlayerData) end)
		if success then warn("Successfully Stored Data") end
	end
end

--// PlayerAdded
Players.PlayerAdded:Connect(function(player)
	LoadStats(player)
	GetPlayerData(player)

	if player.Loaded.Value == true then
		while wait(1) do
			GetCoins(player)
		end
	end
end)

--// PlayerRemoving
Players.PlayerRemoving:Connect(function(player)
	SaveData(player)
end)

--// BindToClose
game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		coroutine.wrap(SaveData)(player)
	end
end)

Before: when I’m hatching pets
image

After when I leave

when i join again


image

You do have Studio API Services Access enabled correct?

i turned it the api service on and look at the images

Maybe try changing this to PlayerData.leaderstats[v.Name] = v.Value
Same with the other ones like,

Click on that error and then send that line of code it takes you to.

Add a check to your for loop. E.g. if value:IsA("IntValue") then

Edit: Make sure to change the IntValue to the class name of your value object.

could you put an example? Like this ClassName = value.ClassName?

Ignore my post above, Your getting that error because PetID is a folder not an actual value, if there is a value called this make sure to give it a unique name.

Edit: A photo of all the values added while your ingame would be helpful.

image

Try this

for index,value in pairs(player.Pets:GetChildren()) do
			if value:IsA("Folder") then
			PlayerData.Pets[value.Name] = {
				Name = value.Name,
				TotalXP = value.TotalXP.Value,
				Equipped = value.Equipped.Value,
				PetID = value.PetID.Value,
				Multiplier1 = value.Multiplier1.Value,
				Multiplier2 = value.Multiplier2.Value,
				Type = value.Type.Value
			}
			print(index,value)
			end
end

Edit: You have 2 folders named donut which means the second folder will overwrite the first one in the PlayerData.

it still only save 1 pet each on the data

Because you have 2 named exactly the same causing it to overwrite, maybe try adding a value of how many of the pet you have and saving that instead of having 2 folders for the same pet.

could you send an example of how to do it?

Just add it to your save table.E.g. Copies = Copies.Value

If you have 2 of the same pets, and you want to save them, you could simply just clone the folder and place it inside of the player’s data within wherever you saved your stuff. Rather than setting, I would clone and insert.

image
i did like this but still

The reason this happens is that you can only have one value per key. This means that if you overwrite the previous Donut data and replace it with a new one each time your loop goes through a Donut. So you might have to turn your data into an array instead of a dictionary. For example:

table.insert(PlayerData["Pets"],{
	Name = value.Name
	TotalXP = value.TotalXP.Value,
	Equipped = value.Equipped.Value,
	PetID = value.PetID.Value
	Multiplier1 = value.Multiplier1.Value,
	Multiplier2 = value.Multiplier2.Value,
	Type = value.Type.Value
}

I hope this somewhat helps your problem.