Datastore issue

So for some reason my data isn’t saving, why is this happening?

local DS = game:GetService("DataStoreService"):GetDataStore("InventoryDS")

game.Players.PlayerAdded:Connect(function(plr)
	wait(.1)

	local inventory = Instance.new("Folder")
	inventory.Name = "Inventory"
	inventory.Parent = plr

	local key = "id-"..plr.userId

	pcall(function()
		local items = DS:GetAsync(key)

		if items then
			for i, v in pairs(items) do
				local item = game:GetSerive("ServerStorage"):FindFirstChild(v, true)
					
				if item then
					item:Clone().Parent = plr:WaitForChild("Inventory")
				end
			end
		end
	end)
end)



game.Players.PlayerRemoving:Connect(function(plr)
	local key = "id-"..plr.userId

	pcall(function()
		local itemsToSave = {}

		for i, v in pairs(plr.Inventory:GetChildren()) do
			if v then
				table.insert(itemsToSave, v.Name)
			end
		end

		DS:SetAsync(key, itemsToSave)
	end)
end)
1 Like

The data is not saving because the server is being shutdown before it has a chance to save. Refer to this similar post.

1 Like