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)
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.
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.