Give Cash Script Is Not Working

There is a bug with my give Cash script where only the last developer product in the script works. (here is an example) In this script, (Which is the script I am using) the only developer product that gives you Cash is 100k (the last script area) local mps = game:GetService(“MarketplaceService”)

mps.ProcessReceipt = function(reciptInfo)
if reciptInfo.ProductId == 980125722 then
local Player = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 1000
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
mps.ProcessReceipt = function(reciptInfo)
if reciptInfo.ProductId == 980126287 then
local Player = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 2500
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
mps.ProcessReceipt = function(reciptInfo)
if reciptInfo.ProductId == 980126614 then
local Player = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 10000
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
mps.ProcessReceipt = function(reciptInfo)
if reciptInfo.ProductId == 980126950 then
local Player = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 25000
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
mps.ProcessReceipt = function(reciptInfo)
if reciptInfo.ProductId == 980127210 then
local Player = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 50000
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
mps.ProcessReceipt = function(reciptInfo)
if reciptInfo.ProductId == 980127849 then
local Player = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 100000
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end

This is happening because you are reassigning the function each time you create the code for each asset, and MarketplaceService can only have one ProcessReceipt function. To fix this, use elseif statements.

mps.ProcessReceipt = function(reciptInfo)
   local Player = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
   if reciptInfo.ProductId == 980125722 then
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 1000
        return Enum.ProductPurchaseDecision.PurchaseGranted
   elseif reciptInfo.ProductId == 980126287 then
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 2500
        return Enum.ProductPurchaseDecision.PurchaseGranted
   elseif reciptInfo.ProductId == 980126614 then
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 10000
        return Enum.ProductPurchaseDecision.PurchaseGranted
   elseif reciptInfo.ProductId == 980126950 then
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 25000
        return Enum.ProductPurchaseDecision.PurchaseGranted
   elseif reciptInfo.ProductId == 980127210 then
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 50000
        return Enum.ProductPurchaseDecision.PurchaseGranted
   elseif reciptInfo.ProductId == 980127849 then
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 100000
        return Enum.ProductPurchaseDecision.PurchaseGranted
   end
end

In future, try to use the code markup feature when making your post to make your code easier to read in the hands of other developers.

1 Like

Ok thanks the issue is solved now!

This could not been a better example to prove that it is important to read the documentation. If you don’t know what you’re doing, how can you expect your code to work? Read the documentation, or more crudely, RTFM. ProcessReceipt documentation.

Unlike other functions you’ve worked with, you are defining a function for ProcessReceipt, not adding one. You may only use one function for ProcessReceipt and you should also use it very carefully at that. If you need different handlers, you can split off into ModuleScripts.

There are code samples in the documentation and even on a separate article explaining how you can work with developer products in your game. Read the documentation first, do your research, before posting topics in this category. Your question can very easily be answered from documentation.

@Eisyan I know it’s just a code sample showing how to fix it but in an actual production environment, for this many if statements that do the same thing, please use a dictionary instead. It’s much more scalable and easy to add products that way. The key can be the ProductId and the value cash to be added. Avoid giant if statements like this if they’re just the same code.

1 Like

I couldn’t agree more, the sample shown was just to give an example of how the issue can be solved. Had I done this, I would have used a dictionary, as well as having used a DataStore to keep the purchases stored.

2 Likes