How to save Values inside a Value ? Like Pets Stats

1. What do you want to achieve?

I want achieve this

wantachive
2. What is the issue?

I always achieve this and my roblox studio always crash

achive
3. What solutions have you tried so far?

My Code:

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore       = DataStoreService:GetDataStore("PlayerData")

local function LoadPlayerData(player)
	
	local leaderstats      = script.leaderstats:Clone()
	leaderstats.Name       = script.leaderstats.Name
	leaderstats.Parent     = player
	local DataFolder       = script.Data:Clone()
	DataFolder.Name        = script.Data.Name
	DataFolder.Parent      = player
	
	local PlayerPrefixData = "Player_".. tostring(player.UserId)
	print("Loading ".. PlayerPrefixData .. " Data")
	local saveData
	
	local DataSucess, DataFail = pcall(function()
		saveData = PlayerDataStore:GetAsync(PlayerPrefixData)
	end)
		
	if DataSucess then
		
		if saveData then
			
			if saveData.Pets then
				for v0,v1 in pairs(saveData.Pets) do
					
					if DataFolder.Pets:FindFirstChild(v0) then
						
						DataFolder.Pets[v0].Value = v1
						
						for v2,v3 in pairs(v0) do
							
							if DataFolder.Pets[v0]:FindFirstChild(v2) then
								
								local PetsInside = DataFolder.Pets[v0]
								
								PetsInside[v2].Value = v3
								
							else
								print("Cant find")
									
								local PetMultiplier  = Instance.new("NumberValue")
								PetMultiplier.Name   = v2
								PetMultiplier.Value  = v3
								PetMultiplier.Parent = DataFolder.Pets[v0]
								
							end
							
						end
						
					else
							
						local OwnPets  = Instance.new("NumberValue")
						OwnPets.Name   = v0
						OwnPets.Value  = v1
						OwnPets.Parent = DataFolder.Pets
						
						print(v0,v1)
						
					end
				end
			end
			
		end
		
	else
			
		print("Error to load ".. PlayerPrefixData .. " Data")
		
	end
	
end

local function SavePlayerData(player)
	
	local PlayerPrefixData = "Player_".. tostring(player.UserId)
	local leaderstats      = player:FindFirstChild("leaderstats")
	local DataFolder       = player:FindFirstChild("Data")
	
	if DataFolder then
			
			local DataSucess = pcall(function()
				PlayerDataStore:UpdateAsync(PlayerPrefixData, function(saveData)
					
					if saveData then
						
						if not saveData.Pets then
							saveData.Pets = {}
						end
						
					else
							
						saveData = {
							Pets = {}
						}
						
					end
					
					for v0,v1 in pairs(DataFolder.Pets:GetChildren()) do
						
						saveData.Pets[v1.Name] = v1.Value
						
						for v2,v3 in pairs(v1:GetChildren()) do
							
							local PetsInside = saveData.Pets[v1.Name]
							
							PetsInside[v3.Name] = v3.Value
							
							wait()
							
							print(saveData.Pets[v1.Name])
							print(saveData.Pets[v1.Name][v3.Name])
							
						end
						
					end
					
					print("Saved ".. PlayerPrefixData .. " Data")
					return saveData
					
				end)
			end)
		
	else
		
		print(PlayerPrefixData.. " not saved")	
		return false
		
	end
	
end

game.Players.PlayerAdded:Connect(function(player)
	LoadPlayerData(player)
end)

game:BindToClose(function()
	for _,player in pairs(game.Players:GetPlayers()) do
		SavePlayerData(player)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	SavePlayerData(player)
end)

This code above, I tried to save the pet statistics but always got fail or crash my roblox studio ;/, <3

2 Likes

Studio is crashing because you’re having a repeat until loop constanly going with no yield. In result, you’re excessevly making a pcall and calling :UpdateAsync(), which is exhausting the script. Maybe this option is great for making sure to save data safely, but it’s too much for the script to handle.

Try removing the repeat until. From my experience :BindToClose() is already enough to be sure that data is saved.

1 Like

Thanks, that’s really why I was crashing, but I’m still not saving the value

gems

Can you check if this is from another script? I can’t seem to find the problem. If not can you dhow the exact line that errors.

The exact line is 115:

This Gems value

geming

Soo, I understand the problem, but I’m unsure what change should you do becauseI don’t exactly understand how the script works.
Perhaps change the PetsInside variable

local PetsInside = v1:GetChildren()

saveData.Pets[v1.Name] is a number value, I assume you’re looking for the container containing the pets (the v3s)

Still with the same error

If u want to take a look in the game.

Pets System.rbxl (41.4 KB)