Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I Need Help With Modifying Values In DataStores?

I started using a plugin called DataStore Editor.

DataStore Editor

I'm trying to make it so my script adds a point every time someone buys this dev product. With the plugin I went to the DataStore, made a key, and made a table to set up for how many times someone has bought it. I'm not sure how to edit that part of the DataStore from a script.

Image

My script:

local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

 

local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
 
local productFunctions = {}

productFunctions[1010354831] = function(receipt, player)
			wait(1)
			--This is where I could add stuff if they bought it.
		return true
	end

local function processReceipt(receiptInfo)
  
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
	local purchased = false
	local success, errorMessage = pcall(function()
		purchased = purchaseHistoryStore:GetAsync(playerProductKey)
	end)

	if success and purchased then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	elseif not success then
		error("Data store error:" .. errorMessage)
	end
 
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then

		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	

	local handler = productFunctions[receiptInfo.ProductId]
 

	local success, result = pcall(handler, receiptInfo, player)
	if not success or not result then
		warn("Error occurred while processing a product purchase")
		print("\nProductId:", receiptInfo.ProductId)
		print("\nPlayer:", player)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
 

	local success, errorMessage = pcall(function()
		purchaseHistoryStore:SetAsync(playerProductKey, true)
	end)
	if not success then
		error("Cannot save purchase data: " .. errorMessage)
	end

	return Enum.ProductPurchaseDecision.PurchaseGranted
end
 
MarketplaceService.ProcessReceipt = processReceipt

1 answer

✓ Accepted answer

Answered on Discord. In order to log the count of purchases you have 2 options:

Option 1, logs for your player, per product Id, a table of purchase Id's. Useful if you need to have access to the purchase Id's at a later stage.

local purchaseData = purchaseHistoryStore:GetAsync(receiptInfo.PlayerId)
if purchaseData[receiptInfo.ProductId] == nil then
	purchaseData[receiptInfo.ProductId] = {}
end
table.insert(purchaseData[receiptInfo.ProductId], {
	PurchaseId = receiptInfo.PurchaseId
})
purchaseHistoryStore:SetAsync(receiptInfo.PlayerId, purchaseData)

Option 2, increment a static number per product Id. Quicker and stores less data, but you won't know your purchase Id's anymore.

local purchaseData = purchaseHistoryStore:GetAsync(receiptInfo.PlayerId)
if purchaseData[receiptInfo.ProductId] == nil then
	purchaseData[receiptInfo.ProductId] = 0
end
purchaseData[receiptInfo.ProductId] += 1
purchaseHistoryStore:SetAsync(receiptInfo.PlayerId, purchaseData)

This discussion is preserved as it was. New answers, comments, and votes are closed.