Datastore only stores 2 items in it?

So I created a datastores that saves when a player makes a purchase for analytics.
Only problem is itll only save the second and first thing they purchase

As you can see this is the saved table, it saves up to [2] and just stops. And I have no idea why

Saved Table
▼  {
                    ["LastReceipt"] =  ▶ {...},
                    ["Receipts"] =  ▼  {
                       [1] =  ▼  {
                          ["BoughtBy"] = USERID,
                          ["Date"] = "DATE",
                          ["ProductId"] = PRODUCTID,
                          ["amountSpent"] = 0
                       },
                       [2] =  ▼  {
                          ["BoughtBy"] = USERID,
                          ["Date"] = "DATE",
                          ["ProductId"] = PRODUCTID,
                          ["amountSpent"] = 0
                       }
                      -- should be another thing here!
                    }
                 }

Code:

local function saveReceipt(player:Player, receipt)
	if receipt and player and ReceiptStore then
		print(receipt)
		local success, errorMessage = pcall(function()

			ReceiptStore:UpdateAsync(player.UserId, function(pastData)
				local storeReceipt = {
					amountSpent = receipt.CurrencySpent,
					BoughtBy = receipt.PlayerId,
					ProductId = receipt.ProductId,
					Date = os.date()
					
				}
				local receipts = {}
				
				if pastData then
					receipts = {
						Receipts = {table.unpack(pastData.Receipts), storeReceipt}, -- update the saves
						LastReceipt = storeReceipt,
					}
					print(receipts)
				else
					receipts = {
						Receipts = {storeReceipt},
						LastReceipt = storeReceipt,
					}
				end
				
				
				warn(receipts)
				return receipts
			end)
		end)
		
		if not success then
			warn(errorMessage)
		else
			warn('RECEIPT WAS SAVED')
		end
	else
		warn('EITHER NO PLAYER, NO RECEIPT OR NO DATASTORE FAILED')
	end
end

Thank you!

Whats the third thing? Looks like your only storing the Receipts and LastReceipt.

You return the receipts variable from the function but it only ever contains two elements.

Yes, well you see in the table it should store another thing but its not.

As you can see here this should update the value adding the new receipt, it should be saved to:

receipts > receipts

but its not saving it there, let alone saving it at all.

Is it entering the pastData block? Do you get the print out?

you want me to print the pastData? I can do that

No i’m just asking if it enters that block of the if statement. But if there is only one past “StoreReceipt” then there would only be 2 elements in the reciept table.

I figured it out! Not exactly though. Where I change the receipts:

Receipts = {table.unpack(pastData.Receipts), storeReceipt}, -- update the saves

This is staying the same, I print it and I see its not updating. I think I might know how to fix it

Yea in the if statement block pertaining to the pastData its just adding another new element. So the count of elements depends on how many past saves there were. Does that make sense?

StoreReceipt
StoreReceipt, StoreReceipt
StoreReceipt, StoreReceipt, StoreReceipt
and so on…

I figured it out! I just have to add the storeReceipt differently, I used table.insert instead and now it works

1 Like