Hi! I’m trying to figure out how to run specific code after a purchase goes through. Essentially what happens is that from client, I fire a RemoteEvent. Once the server gets the remote event, it’ll prompt a GP purchase, and if the GP is purchased, the script will send a remote back. I figured ProcessReceipt would be a good way to do this, but I took a look at a lot of different posts and I feel like they don’t explain it too well (to me atleast.) I’m kind of known for thinking too logically, so I’d love if I could get more simpler terms.
Posts I've looked at
How to use ProcessReceipt? - #16 by BanTech
MarketplaceService.ProcessReceipt
scripting - How can I make something happen when I buy a DevProduct (Roblox LUA) - Stack Overflow
Best way to use ProcessReceipt
Here’s my code I currently have:
local productHandlers = {}
productHandlers[36640175] = function(plr, Product) -- How do I pass this Product variable from the event fired below?
-- Run code for product...?
end
-- All of this function is confusing to me.
MPS.ProcessReceipt = function(receiptInfo)
local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local handler = productHandlers[receiptInfo.ProductId]
if not handler then
error(string.format("No handler is defined for %d", receiptInfo.ProductId))
end
local success, result = pcall(handler, player)
if not success or not result then
local message = table.concat({
"Error occurred while processing a product purchase",
" - ProductId: " .. tostring(receiptInfo.ProductId),
" - Player: " .. player.Name,
}, "\n")
warn(message)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
game.ReplicatedStorage.Process.OnServerEvent:Connect(function(plr, Product) -- See this product variable? How do I pass that through the MPS ProcessReceipt to the code for the product?
local MPS = game:GetService("MarketplaceService")
local GID = game.ReplicatedStorage.Products[Product].Value
MPS:PromptGamePassPurchase(plr, GID)
end)
Any help is appreciated, really looking to try anything haha.
ProcessReceipt is called whenever the user purchases a product. It will be repetitively called until it succeeds (returns Enum.ProductPurchaseDesicion.PurchaseGranted).
However, you’re looking for a gamepass, so you’d need to use UserOwnsGamePassAsync. Call this once when the player joins, since subsequent calls are cached by the engine. Tell the player to rejoin when they’ve bought a gamepass.
You also don’t need any remotes, since you can call PromptGamePassPurchase on the client
Well, see it’s not a game where I need to check when they join. What I’m doing is when they click a button on the UI, it fires to the server. The server will prompt the user to buy this GP, and then check if they bought it. I used to just do a wait function, but I don’t want to have it check before the player actually buys it through the prompt. Is there a way I can like do an await bought?
game.ReplicatedStorage.Process.OnServerEvent:Connect(function(plr, Product)
local MPS = game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory") --Data Stored into "PurchaseHistory"
local GID = game.ReplicatedStorage.Products[Product].Value
MPS:PromptGamePassPurchase(plr, GID)
wait(8)
if MPS:UserOwnsGamePassAsync(plr.UserId, GID) then
-- do this/that
end
end)
UserOwnsGamePassAsync calls are cached through the lifetime of the player being there, so there is no way to know if they bought it or not without having them rejoin.
There is no event for a player purchasing a gamepass.
Gotcha, but say I switched from a GP purchase to like a Developer Product or a shirt maybe, would there be a way for me to check through those?
Yes, that would be ProcessReceipt for products. You’d have to record the purchase in a data store, and refuse any further purchases if they already have the product. Purchases time out after 24 hours.
You can only get shirts through Roblox APIs, I believe, so you can’t do that without a proxy server.
Why would u block the player receipt for every 24 hours, if the player wants to buy the same products 2 times what will happen?
For more det. infos:
local pTable = {}
pTable[123457789] = function()
return {["value"] = 100}
end
MPS.ProcessReceipt = function(receipt)
local requestSuccess, key = pcall(function()
return pTable[receipt.ProductId
end)
if requestSuccess then
local plr = game.players:GetPlayerByUserId(receiptInfo.PlayerId)
plr:FindFirstChild("Coins”).Value += key.value
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
if there are any errors lmk I write code on mobile, hard to see for mistakes
mb lol now I see this Thread was posted 1 year ago xd