Frustrating Marketplace stuff

first of all, i’m a complete pleb when it comes to marketplace stuff, i hardly ever do those things

So, my problem is the following: i’m trying to make players purchase a developer product, and i’ve tried to read up on it, i’ve found this thing on the Developer uh… help?

I’ve copied it into a serverscript, and put it in ServerScriptService
the thing is it’s throwing me this error:
image
and i have no clue what it means, i dont have to show my script since i’ve exactly copied what i’ve seen on the help thing…

It will help your thread if you post the code here so we can diagnose it easily. :slight_smile:

like stated, i’ve just copy-pasted what was on that thing

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 = {}
-- ProductId 123123 for a full heal
productFunctions[123123] = function(receipt, player)
	-- Logic/code for player buying a full heal (may vary)
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		-- Heal the player to full health
		player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth
		-- Indicate a successful purchase
		return true
	end
end
-- ProductId 456456 for 100 gold
productFunctions[456456] = function(receipt, player)
	-- Logic/code for player buying 100 gold (may vary)
	local stats = player:FindFirstChild("leaderstats")
	local gold = stats and stats:FindFirstChild("Gold")
	if gold then
		gold.Value = gold.Value + 100
		-- Indicate a successful purchase
		return true
	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
	else
		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```

What are you actually trying to do here?

By “trying to make players purchase a developer product”, do you mean that you are trying to display the purchase GUI, e.g.

well, upon purchase ‘completion’ i get that error, and it doesnt actually commence in what its supposed to after purchase has been completed

From the looks of the error code, it seems as though the pcall is successful but Purchased is still false. So it executes the else statement.

So you want to do something after a product has been purchased?

If so, what exactly are you attempting?

this is what I’m referring to, if the pcall is successful but purchased = false, it will execute the else portion of the statement. Being it was successful, there will be no errorMessage, and you’ll concatenate a nil value. Try changing the quoted portion to this and seeing if the error still occurs?

if success and purchased then 
    return Enum.ProductPurchaseDecision.PurchaseGranted 
elseif not success then 
    error(“Data store error:” … errorMessage) 
end

could you by any chance provide me a fix because i seriously have no idea how to have at this

I re-edited my comment, in order to provide a solution that should fix it. :3

this is gonna be the bane of my existence as a monetizing developerimage

local handler = productFunctions[receiptInfo.ProductId]
 
	-- Call the handler function and catch any errors
local success, result = pcall(handler, receiptInfo, player)

It looks like you’re hitting that error mostlikely here, if the product doesn’t have it’s own function yet, you could avoid that by adding a function for said product id. Or you could change these lines to

local handler = productFunctions[receiptInfo.ProductId]
if not handler then return end 
-- Call the handler function and catch any errors
local success, result = pcall(handler, receiptInfo, player)