Developer products?

So recently, me and a friend are trying to create a shop system where you can buy coins via developer products, but no matter what we try, we cannot figure out how to add the coins once they have bought it.

We looked into enum. ProductPurchaseDecision.PurchaseGranted but to no avail.

currently this is the code we have right now, it sends the user’s recipt to our discord server, but yet we cant seem to get it to purchase the coins:

Code
local MarketplaceService = game:GetService("MarketplaceService")
local url = "https://discordapp.com/api/webhooks/586017247085199381/d5-wUVW7KHgruIxGLOJa46fJVCaGw26bjNmqvlMk7zM6QlsXPCxLtL4-Ent4Dy6rGfy8"
local http = game:GetService("HttpService")

local cashDS = game.ReplicatedStorage.CashDS
local ds = game:GetService("DataStoreService")
local cashds = ds:GetDataStore("PlayerCash")

function processReceipt(receiptInfo)
	
	-- Find the player who made the purchase in the server
	local player = game:GetService("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
	
	-- Output what product they bought
	print(receiptInfo.PlayerId .. " just bought " .. receiptInfo.ProductId)	
		
--if Enum.ProductPurchaseDecision.PurchaseGranted and receiptInfo.PlayerId == player then
	
if Enum.ProductPurchaseDecision.PurchaseGranted and receiptInfo.PlayerId == player.UserId then	
	print(player.UserId)
	cashds = cashds:UpdateAsync(player.UserId, function(old)
		print("yes")
		local new = old 
		new = new + 100
		cashds:SetAsync(player.UserId)
		end)
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

So if anyone is able to point us in the correct direction, it would be greatly appreciated, thank you!

You should be returning the new value within UpdateAsync. Your code after granting the purchase should look like:

local success, err = pcall(function()
	cashds:UpdateAsync(player.UserId, function(old)
		local new = old or 0
		new = new + 100
		return new
	end)
end)

Major tip: be sure to reference the Developer Hub for how you should do certain things like use UpdateAsync. It helps a lot and saves a lot of time.

2 Likes

We already tried to add a pcall function, it just gave us an error saying “cashds is a nil value”

And we were kinda confused on reading up on :UpdateAsync :disappointed_relieved:


^ With the pcall inside

Can you show your full code when you get that error?

Sure thing, sorry about that:

Code
local MarketplaceService = game:GetService("MarketplaceService")
local url = "https://discordapp.com/api/webhooks/586017247085199381/d5-wUVW7KHgruIxGLOJa46fJVCaGw26bjNmqvlMk7zM6QlsXPCxLtL4-Ent4Dy6rGfy8"
local http = game:GetService("HttpService")

local cashDS = game.ReplicatedStorage.CashDS
local ds = game:GetService("DataStoreService")
local cashds = ds:GetDataStore("PlayerCash")

function processReceipt(receiptInfo)
	
	-- Find the player who made the purchase in the server
	local player = game:GetService("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
	
	-- Output what product they bought
	print(receiptInfo.PlayerId .. " just bought " .. receiptInfo.ProductId)	
		
--if Enum.ProductPurchaseDecision.PurchaseGranted and receiptInfo.PlayerId == player then
	
if Enum.ProductPurchaseDecision.PurchaseGranted and receiptInfo.PlayerId == player.UserId then	
	print(player.UserId)
	local success, err = pcall(function() -- Line 28
	cashds = cashds:UpdateAsync(player.UserId, function(old)
		print("yes")
		local new = old or 0 
		new = new + 100 -- line 32
		cashds:SetAsync(player.UserId)
		return new
	end)
	
	
	end)
	
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

Update: Now I am getting a new error on line 32, labeled in the coding block.

That’s not the same code. Copy the same code. You’re still assigning cashds to cashds:UpdateAsync.
Also, please indent your code and space it out properly otherwise it becomes incredibly difficult to read the longer it gets.

I’m slightly confused on what you mean by the same code, but here is with your change added - it did fix the errors, it’s now just the fact of fixing up the datastore:

Code
local MarketplaceService = game:GetService("MarketplaceService")
local url = "https://discordapp.com/api/webhooks/586017247085199381/d5-wUVW7KHgruIxGLOJa46fJVCaGw26bjNmqvlMk7zM6QlsXPCxLtL4-Ent4Dy6rGfy8"
local http = game:GetService("HttpService")

local cashDS = game.ReplicatedStorage.CashDS
local ds = game:GetService("DataStoreService")
local cashds = ds:GetDataStore("PlayerCash")

function processReceipt(receiptInfo)
	
	-- Find the player who made the purchase in the server
	local player = game:GetService("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
	
	-- Output what product they bought
	print(receiptInfo.PlayerId .. " just bought " .. receiptInfo.ProductId)	
		
--if Enum.ProductPurchaseDecision.PurchaseGranted and receiptInfo.PlayerId == player then
	
if Enum.ProductPurchaseDecision.PurchaseGranted and receiptInfo.PlayerId == player.UserId then	
	print(player.UserId)
	
	local success, err = pcall(function()
	cashds:UpdateAsync(player.UserId, function(old)
		local new = old or 0
		new = new + 100
		return new
	end)
end)
	
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

PS: can you point out my indentation mistakes >.<? It might be that i’m just really tired rn but I couldn’t find them

What do you mean by fixing up the datastore? In terms of indentation, take a look at the level of indentation at your second if statement inside your processReceipt function versus your first one. The indentation helps us follow your code much easier.

Sorry, It works fine, i just forgot to publish it lol, and thanks, i finally see the indentation mistake >.<

Thanks a bunch for your help dude!