DataStore saving bug

I have data store script which should save player bool values names in his inventory folder to data store table, but it prints "ItemsDataSaved!’ but table is empty. Here is script:

local DataStoreService=game:GetService("DataStoreService")
local InventoryDS=DataStoreService:GetDataStore("InventoryDS")
local RS=game:GetService("ReplicatedStorage")


game.Players.PlayerAdded:Connect(function(player)
	local inventory=Instance.new("Folder", player)
	inventory.Name="Inventory"

	local itemsData

	local success, errormessage=pcall(function()
		itemsData=InventoryDS:GetAsync(player.UserId)
	end)

	if success then 
		print("SUCCESS")
	else 
		print(errormessage)
	end


	player.CharacterAdded:Wait()
	wait(4)

	if success and itemsData then
		for i,itemName in ipairs(itemsData) do
			for _,v in pairs(RS.Cases:GetDescendants()) do
				if v:IsA("Model") and v.Name== itemName then
					local itemValue=Instance.new("BoolValue", player.Inventory)
					itemValue.Name=itemName
				end
			end
		end
	end
end)



local function saveData(player)
	local itemsTable={}

	for i,item in ipairs(player.Inventory:GetChildren()) do
		table.insert(itemsTable,item.Name)
	end

	local success,errormessage=pcall(function()
		InventoryDS:SetAsync(player.UserId,itemsTable)
	end) 

	print(table.concat(itemsTable,", "))


	if success then
		print("ItemsDataSaved!")
	else
		print(errormessage)
	end 
end


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




game:BindToClose(function()
	for _,player in pairs(game.Players:GetPlayers()) do
		saveData(player)
		print("btc")
	end
end)

explorercasepreview

player.CharacterAdded:Wait()
wait(4)

That’s your issue right there. Just remove it and it will work

same, the bug is still there. item table seem to become an empty table

It’s working for me after removing the two lines that I mentioned above. It might be the way your adding the items to the inventory. You’re doing it through the server and not the client, correct?

through client :slight_smile: I constantly forget to do it through the server, thanks for the reminder!