Inventory Duplicating On Leave

  1. What do you want to achieve?
    I need to fix my ProfileService Datastore

  2. What is the issue?
    The inventory keeps duplicating whenever I leave and I have no clue why this is happening

  3. What solutions have you tried so far?
    I have looked on the developer hub and have had no luck.

Here Is My Code:

local ProfileService = require(game.ReplicatedStorage.ProfileService)
local Players = game:GetService("Players")
local Profiles = {}

local saveStructure = {
	Wins = 0;
	Cash = 0;
	Inventory = {
	};
}

local PlayerProfileStore = ProfileService.GetProfileStore("DataC", saveStructure)

local function PlayerDataLoaded(player)
	local profile = Profiles[player]

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local folder2 = Instance.new("Folder")
	folder2.Name = "Inventory"
	folder2.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = folder
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = player

	wins:GetPropertyChangedSignal("Value"):Connect(function()
		profile.Data.Wins = wins.Value
	end)
	
	cash:GetPropertyChangedSignal("Value"):Connect(function()
		profile.Data.Cash = cash.Value
	end)
	
	for i,v in ipairs(profile.Data.Inventory) do
		for _, item in pairs(game.ServerStorage.Inventory:GetDescendants()) do
			if item.Name == v[1] then
				local Value = game.ServerStorage.ItemValue:Clone()
				Value.Name = v[1]
				Value.Equipped.Value = v[2]
				Value.Value = v[3]
				Value.Parent = folder2
			end
		end
	end
	
	if not folder2:FindFirstChild("Classic") then
		
		local Value = game.ServerStorage.ItemValue:Clone()
		Value.Name = "Classic"
		Value.Value = "Bomb"
		Value.Equipped.Value = true
		Value.Parent = folder2
		
		local Value = game.ServerStorage.ItemValue:Clone()
		Value.Name = "Classic"
		Value.Value = "Explosion"
		Value.Equipped.Value = true
		Value.Parent = folder2
		
	end

	spawn(function()
		local profile = Profiles[player]

		if profile ~= nil then		
			
			wins.Value = profile.Data.Wins
			cash.Value = profile.Data.Cash
		end
	end)
end

local function PlayerAdded(player)
	local profile = PlayerProfileStore:LoadProfileAsync("Player_" .. player.UserId, "ForceLoad")
	if profile ~= nil then
		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick("Your profile has been loaded remotely. Please rejoin.")
		end)

		if player:IsDescendantOf(Players) then
			Profiles[player] = profile
			PlayerDataLoaded(player)
		else
			profile:Release()
		end
	else
		player:Kick("Unable to load saved data. Please rejoin.")
	end
end

for _, player in ipairs(Players:GetPlayers()) do
	spawn(function()
		PlayerAdded(player)
	end)
end

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player)
	local profile = Profiles[player]
	if profile ~= nil then
		
		profile.Data.Inventory = {}
		
		print(#player:WaitForChild("Inventory"):GetChildren())

		for i, v in pairs(player:WaitForChild("Inventory"):GetChildren()) do
			
			table.insert(profile.Data.Inventory, {v.Name, v.Equipped.Value, v.Value})
			
		end
		
		profile:Release()
		
	end
end)

return Profiles

Please Help!