Player Inventory DataStore Script Not Working

This script saves the player’s inventory. No errors are coming up, but it does not save.

    local DataStore = game:GetService("DataStoreService"):GetDataStore("Inventory_Saving")

    local replicatedStorage = game:GetService("ReplicatedStorage")

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

	local inventory = Instance.new("Folder")
	inventory.Name = "CardInventory"
	inventory.Parent = player
	
	local data
	local succ, er = pcall(function()
		data = DataStore:GetAsync("CardInventory-"..player.UserId)
		print("recovered inventory")
	end)
	
	if succ and data then
		for _, cardName in pairs(data) do
			if replicatedStorage.Cards:FindFirstChild("cardName") then
				local cardValue = Instance.new("StringValue")
				cardValue.Name = cardName
				cardValue.Parent = inventory
			end
		end
	end
end)

	game.Players.PlayerRemoving:Connect(function(player)
		
		local cardTable = {}
		
		for _, card in pairs(player.CardInventory:GetChildren()) do
			table.insert(cardTable,card.Name)
			print(card.Name)
		end
	
		local succ, er = pcall(function()
			DataStore:SetAsync("CardInventory-"..player.UserId,cardTable)
		end)
	
	if succ then 
		print("saved")
	else
		print(er)
	end
end)

All the prints are working except “saved”.
Thanks!

1 Like

if replicatedStorage.Cards:FindFirstChild(“cardName”) then

Are you searching for an object named “cardName”?

1 Like

Yes it’s to check if it’s an actual card

you’re checking for an actual object named
“cardName” there, not the value from the table

In alvinblox’s video he’s doing the same thing

do this:

if replicatedStorage.Cards:FindFirstChild(cardName) then

instead of this:

if replicatedStorage.Cards:FindFirstChild(“cardName”) then