Hey everyone!
I’m currently trying to make a script so that when a player purchases a devproduct it increases a value in a leaderstat they have.
I’ve done this so far in a LocalScript under a TextButton:
local player = game.Players.LocalPlayer
local productId = 1163772407
local MarketPlaceService = game:GetService("MarketplaceService")
script.Parent.MouseButton1Click:Connect(function()
MarketPlaceService:PromptProductPurchase(player, productId)
end)
I’m quite unsure where to go from here and have searched around and found nothing, let me know if you can help. Thanks!
1 Like
You use ProcessReceipt
for that, the article has an example of how you could do it and work from there
I’ve read over this from start to finish multiple times and I’m still quite confused about how to use it because of how long and complex the script example is. Could you explain how to script this or provide a script that does so?
All you really need from that article is this
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local productFunctions = {}
productFunctions[456456] = function(receipt, player)
-- Logic/code for player buying 100 gold (may vary)
local stats = player:FindFirstChild("leaderstats")
local gold = stats and stats:FindFirstChild("Gold")
if gold then
gold.Value = gold.Value + 100
-- Indicate a successful purchase
return true
end
end
-- The core 'ProcessReceipt' callback function
local function processReceipt(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local handler = productFunctions[receiptInfo.ProductId]
local success, result = pcall(handler, receiptInfo, player)
if not success or not result then
warn("Error occurred while processing a product purchase")
print("\nProductId:", receiptInfo.ProductId)
print("\nPlayer:", player)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
MarketplaceService.ProcessReceipt = processReceipt
It’s recommended to use a dictionary to store functions for what happens when the player buys a dev product, all this does is when you buy ad ev product, it checks if the player who bought is still in the server and if they aren’t, tell the game the product has not been granted yet, then just try to get the function from the productFunctions dictionary and g ive it the arguments it needs and if the function doesn’t exist in the dictionary or an error happened in the function, then display warnings and tell the game it was not granted. If all goes well, tell the game the purchase was granted.
You cna make more than one function, just copy the
productFunctions[456456] = function(receipt, player)
-- Logic/code for player buying 100 gold (may vary)
local stats = player:FindFirstChild("leaderstats")
local gold = stats and stats:FindFirstChild("Gold")
if gold then
gold.Value = gold.Value + 100
-- Indicate a successful purchase
return true
end
end
And tweak it around, making sure ot change 456456
to the id of your dev product and tweaking the code of the function, make sure there’s a return true
2 Likes
I’ve put the following in a Script in ServerScriptService:
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local productFunctions = {}
productFunctions[1163772407] = function(receipt, player)
local stats = player:FindFirstChild("leaderstats")
local points = stats and stats:FindFirstChild("Points")
if points then
points.Value = points.Value + 5
return true
end
end
local function processReceipt(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local handler = productFunctions[receiptInfo.ProductId]
local success, result = pcall(handler, receiptInfo, player)
if not success or not result then
warn("Error occurred while processing a product purchase")
print("\nProductId:", receiptInfo.ProductId)
print("\nPlayer:", player)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
MarketplaceService.ProcessReceipt = processReceipt
Will this work?
1 Like
Should work since I don’t really see any mistakes hopefully
It’s not working, unfortunately.
Add a print in the Processreceipt function to see if it’s getting through
Done, it’s still not printing it.
Do you have a donation board in your game by any chance?
1 Like
Yes, I do. (30 ch.rsssssssssss)
That’s why it doesn’t work then, most donation boards have a custom processReceipt callback connected, a single script can set the ProcessReceipt callback, so yours is being overwritten. Your code does work, but it’s never ran cause of it. you’ll need to combine yours and the processreceipt of the board
1 Like
Ah, you’re right. I’ll remove the donation board and test it.
So your saying that there must only be One ProcessReceipt in the whole game?
Yes
It’s a callback, you cannot have more than one or else the one that has been set most recent will take priority
Worked, thanks! (30 ch.rssssssssssssssssssssssss)
2 Likes
Anytime! If you have anymore issues don’t be afraid to make another post!
1 Like
Embat, thank you so much. I was struggling to figure out why my dev. products where not fully working. Once I saw your post about the donation board I disabled its script and tested again and it worked! Thank you again for pointing that out!
1 Like