Why doesn't this dev product script not work?

  1. What do you want to achieve?

I want to achieve a working dev product script that works.

  1. What is the issue?

The issue is it doesn’t work with no errors it just doesn’t give the player the amount of currency.

  1. What solutions have you tried so far?

Finding a solution.

local MarketplaceService = game:GetService("MarketplaceService")

function getPlayerFromId(id)
	for i,v in pairs(game.Players:GetChildren()) do
		if v.userId == id then
			return v
		end
	end
	return nil
end

MarketplaceService.ProcessReceipt = function(receiptInfo)
	local productId = receiptInfo.ProductId
	local playerId = receiptInfo.PlayerId
	local player = getPlayerFromId(playerId)

	if productId == 1232282811 then
		
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 10000
		
	elseif productId == 1232282877 then
		
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 100000
		
	elseif productId == 1232282925 then
		
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1000000

	elseif productId == 1232282987 then
		
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 10000000

	elseif productId == 1232283029 then
		
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 100000000
		
	elseif productId == 1232283060 then
		
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1000000000
	end
	
	return Enum.ProductPurchaseDecision.PurchaseGranted		
end

Thank you in advance!

1 Like

Does it even do anything at all? And is there any errors?

This may be due to the code running inside a LocalScript, multiple scripts listening for MarketplaceService.ProcessReceipt, and player, leaderstats, Strength being nil. I tried cleaning the code above in hopes of fixing the issue(if an instance is nil, the console will show a warning):

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

--[product id] = amount of strength
local products = {
	[1232282811] = 10000,
	[1232282925] = 1000000,
	[1232282987] = 10000000,
	[1232283029] = 100000000,
	[1232283060] = 1000000000
}

MarketplaceService.ProcessReceipt = function(receiptInfo)
	--pcall in case it fails
	local Success, Error = pcall(function()
		--there is a function for this
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
		local leaderstats = player:FindFirstChild("leaderstats")
		local Strength = leaderstats:FindFirstChild("Strength")	
		
		local amount = products[receiptInfo.ProductId]
		Strength.Value += amount or 0 --if the product isn't part of the dictionary, we ignore it.
	end)
	
	if Success then 
		return Enum.ProductPurchaseDecision.PurchaseGranted	
	else 
		warn(Error)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end 
end
1 Like

You never prompted the purchase I think. Or I must’ve missed that line.

local function promptPurchase()
	local players = --// Your player(s) here
	MarketplaceService:PromptProductPurchase(player, productID)
end

I suggest you take a look at ROBLOX’s article about this as it is very detailed.

1 Like

I already have a script that prompts the product

I will try that code when I get on pc tho, thank you!

Is there any way I could have like more ids but it like increases Coins? So there is strength and Coins like how would I put that in the table?

I tried this but then it doesn’t work! The first code you sent worked!

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

--[product id] = amount of strength
local products = {
	[1232282811] = 10000,
	[1232282877] = 100000,
	[1232282925] = 1000000,
	[1232282987] = 10000000,
	[1232283029] = 100000000,
	[1232283060] = 1000000000
}
--Gems--
local products2 = {
	[1232275748] = 10000,
	[1232275772] = 100000,
	[1232275792] = 1000000,
	[1232275869] = 10000000,
	[1232275957] = 100000000,
	[1232275978] = 1000000000
}
--Coins--
local products3 = {
	[1232272876] = 10000,
	[1232272728] = 100000,
	[1232272939] = 1000000,
	[1232273020] = 10000000,
	[1232273262] = 100000000,
	[1232273458] = 1000000000
}

MarketplaceService.ProcessReceipt = function(receiptInfo)
	--pcall in case it fails
	local Success, Error = pcall(function()
		--there is a function for this
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
		local leaderstats = player:FindFirstChild("leaderstats")
		local Strength = leaderstats:FindFirstChild("Strength")	
		local Gems = leaderstats:FindFirstChild("Gems")	
		local Coins = leaderstats:FindFirstChild("Coins")	

		local amount = products[receiptInfo.ProductId]
		Strength.Value += amount or 0 --if the product isn't part of the dictionary, we ignore it.
		
		local amount2 = products2[receiptInfo.ProductId]
		Gems.Value += amount or 0 --if the product isn't part of the dictionary, we ignore it.
		
		local amount3 = products3[receiptInfo.ProductId]
		Coins.Value += amount or 0 --if the product isn't part of the dictionary, we ignore it.
	end)

	if Success then 
		return Enum.ProductPurchaseDecision.PurchaseGranted	
	else 
		warn(Error)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end 
end

Never mind, worked my way around it! Thank you!

This is sort of not working, when the player buys the product it works. But if they leave and join back it goes back to 0! I already have a datastore that works, dw about that.