Developer Product doesn't give the rewards to the player

Hey! So I made developer products that are supposed to give cash to a player in the game.
When you click the button, and buy the developer product, nothing happens.

Here’s the script that I have:

local MarketplaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")
local SG = game:GetService("StarterGui")
local productID = 1347531300

local function processReceipt(receiptInfo)
	
	local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if player then -- Rewards
		game.Players.PlayerAdded:Connect(function(player)
			local cash = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")
			cash.Value = cash.Value * 10
		end)
	end
	
end

MarketplaceService.ProcessReceipt = processReceipt

How to fix it?

1 Like

you don’t need

game.Players.PlayerAdded:Connect(function(player)

As it adds a listener to when a player is added.

Also

game.Players.LocalPlayer

Will never work on the server, and ProcessReceipt has to be on the sever.

The docs has the syntax for handling purchases here.

1 Like
local MarketplaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")
local SG = game:GetService("StarterGui")
local productID = 1347531300

local function processReceipt(receiptInfo)
	
	local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if player then -- Rewards
		if receiptInfo.ProductId == productId then
			local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
			cash.Value = cash.Value * 10
		end
	end
	
end

MarketplaceService.ProcessReceipt = processReceipt
1 Like

This works for a game with only one devproduct, a better solution is to have a dictionary, the key being the id, and the value being a function to use the product, like in the docs.

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

products = {}
products[1347531300] = function(receipt, player) 
	-- Multiplies a player's cash by a factor of 10
	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	cash.Value = cash.Value * 10
end

local function processReceipt(receiptInfo)
	local userId = receiptInfo.PlayerId
	local productId = receiptInfo.ProductId
	local player = Players:GetPlayerByUserId(userId)
	
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local handler = productFunctions[productId]
	local success, result = pcall(handler, receiptInfo, player)
	if success then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	else
		warn("Failed to process receipt:", receiptInfo, result)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

MarketplaceService.ProcessReceipt = processReceipt

This makes it really easy to add more products and fix any bugs in existing ones.

1 Like

I agree. I myself store the IDs in a module so it is accessible by other members of the team.

You have to put the function in the server script.