Script for Dev Product wont work,

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

-- 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[1189071715] = function(receipt, player)
	-- Logic/code for player buying 100 gold (may vary)
	local stats = player:FindFirstChild("leaderstats")
	local gold = stats and stats:FindFirstChild("Gems")
	if gold then
		gold.Value = gold.Value + 100
		-- Indicate a successful purchase
		return print('yo')
	end
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

This script is taken directly from the DevHub. I had my own script, but that did not work either. I tried this and it refuses to give me my gems.

3 Likes

Not enough information. “It doesn’t work” doesn’t explain the problem. The DevHub code sample makes sure to add console messages as much as possible so you can track down error sources. Is there anything in the console representing an issue message? Where exactly does your code stop working (what line and what code is on that line)?

2 Likes

Alright. I have both a local script and server script (which is the one above). The local script is just the basic stuff, prompting the purchase. There isnt an error message in the console nor does the code stop. it just doesnt give me the gems.

1 Like

It gives this error when I try: Error occurred while processing a product purchase

Change this part

For this

print('yo')
return true

If it still doesn’t work, you see that it can delay, destroy or inactivate the code.

I already changed that and it still doesnt work. I accidentally placed that part of code there.

1 Like

Ok so I tested from another game place and it seems to work there. I have another developer product server handler for pets in a seperate module. Would that be the reason why my product isnt working?

EDIT: Moved the server script to another main handler i had where the Purchases would be initialized. And it worked.

1 Like