Pet Saving System Not Replicating To Client

Did roblox make a change, or am I missing something?
My system prints the table fine on leave, and then on load it still prints the same table yet there is some error inside the PlayerAdded function that is causing it to break.
Can someone review this code and see if they find any errors?

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		
		if not plr:FindFirstChild("DATA FAILED TO LOAD") then
			
			local equippedPetsContainer = Instance.new("Folder")
			equippedPetsContainer.Name = "EQUIPPED PETS"
			equippedPetsContainer.Parent = char
			
			for _, equippedValue in pairs(plr:WaitForChild("PetsEquipped"):GetChildren()) do
				if equippedValue.Value then
					create(plr, equippedValue)
				end
			end
		end
	end)

	local dataFailedWarning = Instance.new("StringValue")
	dataFailedWarning.Name = "DATA FAILED TO LOAD"

	local success, plrData = nil, nil
	while true do
		success, plrData = pcall(function()
			print(ds:GetAsync(plr.UserId))
			return ds:GetAsync(plr.UserId)
		end)
		task.wait(0.02)
		if not success then
			dataFailedWarning.Parent = plr
		else
			break
		end
	end
	dataFailedWarning:Destroy()

	if not plrData then
		plrData = {
			Inventory = {};
			Equipped = {};
		}
	end
	
	local inventory = Instance.new("Folder")
	inventory.Name = "PetsInventory"
	inventory.Parent = plr
	
	local equipped = Instance.new("Folder")
	equipped.Name = "PetsEquipped"
	equipped.Parent = plr

	for _, pet in pairs(plrData.Inventory) do
		local foundPet = pets:FindFirstChild(pet, true)

		if foundPet and foundPet.Parent.Parent == pets then
			local petValue = Instance.new("ObjectValue")
			petValue.Value = foundPet
			petValue.Parent = inventory 
			
		end
	end
	
	for i = 1, config.MaxPetsEquipped do
		
		local equippedValue = Instance.new("ObjectValue")
		equippedValue.Name = i
		
		local linkedPet = Instance.new("ObjectValue")
		linkedPet.Name = "LINKED PET"
		linkedPet.Parent = equippedValue
		
		local rarity = Instance.new("StringValue")
		rarity.Name = "RARITY"
		rarity.Parent = equippedValue
		
		equippedValue:GetPropertyChangedSignal("Value"):Connect(function()
			local newValue = equippedValue.Value
			
			if linkedPet.Value then
				linkedPet.Value:Destroy()
			end

			if newValue then
				create(plr, equippedValue, rarity)
			end
		end)
		
		if plrData.Equipped[i] then
			local foundPet = pets:FindFirstChild(plrData.Equipped[i], true)
			
			if foundPet and foundPet.Parent.Parent == pets then
				equippedValue.Value = foundPet
			end
		end
		equippedValue.Parent = equipped
	end
end)

Does it say anything on the output?

If not,
Try to work out where the error is and put

print("Script Loaded Successfully!")

somewhere after it.

Okay, the exact part of code where it is erroring is this portion

for _, equippedValue in pairs(plr:WaitForChild("PetsEquipped"):GetChildren()) do
				if equippedValue.Value then
					print("3")
					create(plr, equippedValue)
				end
			end

The code is not recognizing the PetsEquipped because the PetsEquipped is… empty.

Assuming that plr is the player you’ve collected, put PetsEquipped (with children of course) into ReplicatedStorage/First, and add a line that moves it into the player, to ensure that the children still exist.

That wouldn’t make sense, as I am creating the folder and then using WaitForChild. Another thing is I only started facing this issue AFTER I created a new value inside the pet, I can provide the other script if that would help.

Just edit it, to FindFirstChild, then do the ReplicatedFirst bit?

Or instead of creating the value, put the value alone inside ReplicatedFirst THEN move it in.

Tried this solution, still the same error.

Well I’m not very good with scripting, I just know the basics. You’ll have to see if there’s anybody else who can help you further.

Sorry if I wasn’t of much help :frowning:

Ah no worries man, I’ll keep looking over the code.

1 Like