How to add stuff to tables then save them in datastore

  1. everytime you purchase something it adds to the table and that it saves

  2. i dont know how or if its possible that you can save a table when adding a value without knowing what the value is gonna be

  3. i read so much but nothing helped

local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
local purchases = {}
local function saveData(player) -- The functions that saves data
	local success, err = pcall(function()
		purchaseHistoryStore:SetAsync(player.UserId, purchases) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
		print(purchases)
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

players.PlayerAdded:Connect(function(plr)
	local success, purchases = pcall(function()
		return purchaseHistoryStore:GetAsync(plr.UserId)
	end)
	if success then

		print(purchases)
	end
end)

players.PlayerRemoving:Connect(function(plr)
	saveData(plr)
end)

promptPurchaseEvent.OnServerEvent:Connect(function(plr, id, item)
	if not id then
		return
	end
	if item then
		mps:PromptPurchase(plr, id)
	end

end)

local MarketplaceService = game:GetService("MarketplaceService")

local function onPromptPurchaseFinished(player, assetId, isPurchased)
	if isPurchased then
		print(player.Name, "bought an item with AssetID:", assetId)
		purchases[#purchases+1] = assetId
		saveData(player)
	else
		print(player.Name, "didn't buy an item with AssetID:", assetId)
	end
end

Thank you

Hi, for first I noticed that you have a variable called purchases, and in this line:

	local success, purchases = pcall(function()
		return purchaseHistoryStore:GetAsync(plr.UserId)
	end)
	if success then

		print(purchases)
	end

You load that information, but you should edit the “global” variable, I mean don’t use local; remove it.

What I understood is that you want to know if various types of values can be indexed without knowing what that value is, right?

Yes that is possible.
And if you want to know more about that value specific a extended context, this can be in the same index! Like this:

local purchases = {
	[{ context = 'purchase' }] = assetId
}

Maybe you already know this.

Its like if the player purchases something it adds to the table and then it saves
and its a catalog game so u cant know the ids already
and the variable called purchases is the table
and i dont know how to remove the local

What do you mean with you can’t know the value?
I see in that script that you are indexing it in that table and saving it.
Corresponding to the purchases variable, I don’t meant to remove the local exactly, after the success set it.

i also noticed i was wrong about not knowing the value but i want to save the value(s)

can you help me?
i need to save the values
and create an image for every single one if u bought it specifically in the game

So you want to save it and if it was purchased in the game (detected a saved data) you load an image?

yea kinda and it makes a new image evertime someone purchases something with for i