I am in the process of making a GUI that you can buy Cash through with robux. I have got the buttons working to pop up the purchase correctly, but I am having a problem with getting the cash to be added on some of the purchases. The first two buttons (250 and 500) correctly give you the cash after you purchase, but the other buttons give you nothing when you complete the purchase. I feel pretty dumb at this point, and I am guessing I missed something very simple in my script. I will post my script I am using to check for purchases below. https://imgur.com/a/sbpbcYg
local MarketplaceService = game:GetService("MarketplaceService")
--[[ Cash ID's ]]--
Cash250 = 995850785
Cash500 = 995851074
Cash1000 = 995851288
Cash4000 = 995851543
Cash8000 = 995851834
Cash16000 = 995853514
Cash32000 = 995853729
Cash64000 = 995853920
--[[ Process Receipt ]]--
MarketplaceService.ProcessReceipt = function(receiptInfo)
local pid = receiptInfo.PlayerId
local dpid = receiptInfo.ProductId
for _,plr in pairs(game.Players:GetPlayers()) do
if plr.UserId == pid then
local plrd = plr.leaderstats.Cash
if dpid == Cash250 then
plrd.Value = plrd.Value + 250
elseif dpid == Cash500 then
plrd.Value = plrd.Value + 500
elseif pid == Cash1000 then
plrd.Value = plrd.Value + 1000
elseif pid == Cash4000 then
plrd.Value = plrd.Value + 4000
elseif pid == Cash8000 then
plrd.Value = plrd.Value + 8000
elseif pid == Cash16000 then
plrd.Value = plrd.Value + 16000
elseif pid == Cash32000 then
plrd.Value = plrd.Value + 32000
elseif pid == Cash64000 then
plrd.Value = plrd.Value + 64000
end
end
end
--// Purchase Granted
return Enum.ProductPurchaseDecision.PurchaseGranted
end```
I see the mistake. It’s because instead of elseif dpid (for the 1000, 4000, 8000, 16000, 32000, 64000 cash values), you put elseif pid. Only the 250 and 500 cash values work because they have dpid in front of them. I think you just got your variables mixed up. Remember - the dpid variable stands for the ProductId, which is the one you should have. The pid variable is for the PlayerId - the one you shouldn’t have after elseif.
Let me know if you need clarification, or if this helped you!
There’s a few opportunities to make this code more optimized.
local MarketplaceService = game:GetService("MarketplaceService")
--[[ Cash ID's ]]--
-- Cash ID to Cash amount dictionary
cashDict = {
[995850785] = 250,
[995851074] = 500,
[995851288] = 1000,
[995851543] = 4000,
[995851834] = 8000,
[995853514] = 16000,
[995853729] = 32000,
[995853920] = 64000
}
--[[ Process Receipt ]]--
MarketplaceService.ProcessReceipt = function(receiptInfo)
local pid = receiptInfo.PlayerId
local dpid = receiptInfo.ProductId
local plr = game:GetService("Players"):GetPlayerByUserId(pid)
local cashAmount = cashDict[dpid]
if plr and cashAmount then
local plrd = plr.leaderstats.Cash
plrd.Value = plrd.Value + cashAmount
end
--// Purchase Granted
return Enum.ProductPurchaseDecision.PurchaseGranted
end
You can use Players:GetPlayerByUserId() to get the Player object of the player who made the purchase, which is more efficient than iterating over every player until you find the correct player.
Additionally, you can use a key-value dictionary, so instead of having a long chain of if-then-else statements, you can just plug in the product ID and get the cash amount out in a single statement. This will make the code both more compact and easy to read, as well as making it easier to scale purchase options in case you want to add more dev products to buy cash.