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

Why does my devproduct fire more than once?

So I made a devproduct that gives you 1000 coins ingame the first time you purchase it, it works fine and as expected but the second time you buy it, it gives you 1k coins twice and the third time you buy it, it gives you 1k coins 3 times and so on. Does anyone know what is going on and how i can stop it? This code is from the offical roblox dev site (https://developer.roblox.com/en-us/articles/Developer-Products-In-Game-Purchases) I'm pretty sure i didn't miss anything, could anyone please point out where i went wrong?

my code:

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

-- Function to prompt purchase of the developer product
local function promptPurchase(player,productID)
	MarketplaceService:PromptProductPurchase(player, productID)
end

-- Data store for tracking purchases that were successfully processed
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")

-- Table setup containing product IDs and functions for handling purchases
local productFunctions = {}

productFunctions[1157612529] = function(receipt, player)
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1000
end

-- The core 'ProcessReceipt' callback function
local function processReceipt(receiptInfo)

	-- Determine if the product was already granted by checking the data store  
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
	local purchased = false
	local success, errorMessage = pcall(function()
		purchased = purchaseHistoryStore:GetAsync(playerProductKey)
	end)
	-- If purchase was recorded, the product was already granted
	if success and purchased then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	elseif not success then
		error("Data store error:" .. errorMessage)
	end

	-- Find the player who made the purchase in the server
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		-- The player probably left the game
		-- If they come back, the callback will be called again
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	-- Look up handler function from 'productFunctions' table above
	local handler = productFunctions[receiptInfo.ProductId]

	-- Call the handler function and catch any errors
	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

	-- Record transaction in data store so it isn't granted again
	local success, errorMessage = pcall(function()
		purchaseHistoryStore:SetAsync(playerProductKey, true)
	end)
	if not success then
		error("Cannot save purchase data: " .. errorMessage)
	end

	-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

-- Set the callback; this can only be done once by one script on the server! 
MarketplaceService.ProcessReceipt = processReceipt

game.ReplicatedStorage.RemoteEvents.devproduct.OnServerEvent:Connect(function(player,thing)
	local id
	if thing == "plus1kcoins" then
		id = 1157612529
	end
	promptPurchase(player,id)
end)

1 answer

You need to get the product information. To get information for a specific product, use the GetProductInfo() function with a second argument:

local MarketplaceService = game:GetService("MarketplaceService")
 
local productID = 0000000  -- Change this to your developer product ID
 
local productInfo = MarketplaceService:GetProductInfo(productID, Enum.InfoType.Product)

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