Developer Products and Datastore2

Hi, what I am trying to do is that when the player buys coins from a developer product, it adds the amount to the players coin amount. Here is my code:

local MarketplaceService = game:GetService("MarketplaceService")
local Datastore2 = game.ServerScriptService.PlayerDataStore2

local function processReceipt(receptInfo)
	local player = game:GetService("Players"):GetPlayerByUserId(receptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	local coinStore = Datastore2("Coins", player)
	
		coinStore:Increment(50)

	return Enum.ProductPurchaseDecision.PurchaseGranted

end

MarketplaceService.ProcessReceipt = processReceipt()

GUI Script

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

local fiftyCoinsId = 981008834

script.Parent.MouseButton1Click:Connect(function(promptPurchase)
	local player = Players.LocalPlayer
	MarketplaceService:PromptProductPurchase(player, fiftyCoinsId)
end)

Thank you for the help!

You don’t need the parentheses after processReceipt.

MarketplaceService.ProcessReceipt = processReceipt

So as Vong pointed out, you need to remove the parenthesis. This is important because when this line gets evaluated, what you’re basically doing is calling the function and asking it to return a value which will get assigned to ProcessReceipt. You instead need to make it so that MarketplaceService.ProcessReceipt holds a reference to your processReceipt function.

The next issue is that if “PlayerDataStore2” is a module, then you need to require it. Simply referencing it isn’t enough because you don’t actually get back any of the functions it has, so this’ll lead to an error of you trying to call a non-function value.

You should be doing your own debugging first here. Read errors in the developer console and refer to documentation, be it DataStore2 for use information and examples or the Developer Hub to confirm that you’re using other API correctly.

Playerdatastore2 isn’t a module, it’s a script. Should I switch it to be a module?

I don’t know what PlayerDataStore2 is though, that’s the problem. Is that the actual DataStore2 source? It should be a module if so, not a script. If it’s something different though, then you need to require the actual DataStore2 module in your game.

That’s the issue. I didn’t know that you need to require the Datastore2 module. Playerdatastore2 is where the coinstore is created. Thank you!