Data Duplicates for some reason

Hello! So I tried following the DataStore episode of polarisprog’s pet sim x series (https://www.youtube.com/watch?v=Rgqy6gXHc-M&list=PLH1di03gos6ZuOcenzr5QSvkaKbdBIWcC&index=5), and I encountered an error of which the values itself in the leaderstats produce duplicates.

This is the code I used for the save data function by the way:

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Official")

local playerData = script.PlayerData

local function SaveData(player)
	local data = {}
	for _, folder in pairs(player:GetChildren()) do
		if playerData:FindFirstChild(folder.Name) then
			if playerData[folder.Name]:GetAttribute("SaveChildren") == true then
				data[folder.Name] = {}
				if playerData[folder.Name]:GetAttribute("SaveChildrenValues") == true then
					for _, child in pairs(folder:GetChildren()) do
						if not child:GetAttribute("DoNotSaveValue") then
							table.insert(data[folder.Name], {child.Name, child.Value, child.ClassName})
						end
					end
				else
					for _, child in pairs(folder:GetChildren()) do
						if not child:GetAttribute("DoNotSaveValue") then
							table.insert(data[folder.Name], {child.Name, child.ClassName})
						end
					end
				end
			end
		end
	end
	
	local success, errorMsg = pcall(function()
		dataStore:SetAsync(player.UserId.."Key", data)
	end)
	
	if success then
		print("Saved Data")
	else
		warn(errorMsg)
	end
end

local function LoadData(player)
	for _, v in pairs(playerData:GetChildren()) do
		v:Clone().Parent = player
	end
	local data
	local success, errorMsg = pcall(function()
		data = dataStore:GetAsync(player.UserId.."Key")
	end)
	if errorMsg then
		warn(errorMsg)
		player:Kick("Could not save data")
	end
	if data then
		for i, v in pairs(data) do
			if #v > 0 then
				for x, c in pairs(v) do
					local value
					if c[3] == nil then
						value = Instance.new(tostring(c[2]))
					else
						value = Instance.new(tostring(c[3]))
					end
					value.Name = c[1]
					value.Value = c[2]
					value.Parent = player[tostring(i)]
				end
			end
		end
	end
end

local function PlayerRemoving(player)
	SaveData(player)
end

players.PlayerAdded:Connect(LoadData)
players.PlayerRemoving:Connect(PlayerRemoving)
game:BindToClose(function()
	for _, player in pairs(players:GetPlayers()) do
		coroutine.wrap(SaveData)(player)
	end
end)

Game settings for security:


Hierarchy in ServerScriptService:
image
Attributes for each folder in PlayerData:
image
image
image
image
Incase you need a more indepth look at the game, here is the game file:
test.rbxl (317.0 KB)
I tried to find every solution and check if there are any errors in the output (there werent, nor there were any warnings). Can anyone help?

1 Like