Gamepass multiple ID's assistance

I have a working gamepass script under serverscriptservice that sells a currency. I was wondering if I wanted to add a second gamepass, do I need another script, or can I add to this script?

local MPS = game:GetService("MarketplaceService")

MPS.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 23366248 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Rubies.Value = player.leaderstats.Rubies.Value + 500
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

Will adding to this cause an issue, or what should it look like to add a second currency item. Something like this below?

local MPS = game:GetService("MarketplaceService")

MPS.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 23366248 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Rubies.Value = player.leaderstats.Rubies.Value + 500
		return Enum.ProductPurchaseDecision.PurchaseGranted

if receiptInfo.ProductId == 1456783 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + 500
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

Thanks

You can’t have multiple of ProccessReceipt callbacks in your game.

What you want to do is construct a table:

local gamepassTable = {
  [00000] = function(receiptInfo)
         local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
         -- your stuff
         return Enum.ProductPurchaseDecision.PurchaseGranted
  end,
  [11111] = function(receiptInfo)
         local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
         -- your stuff
         return Enum.ProductPurchaseDecision.PurchaseGranted
  end,
}

local MPS = game:GetService("MarketplaceService")

MPS.ProcessReceipt = function(receiptInfo)
      return gamepassTable[receiptInfo.ProductId](receiptInfo)
end
1 Like

Thanks Ill try this. Turns out my original script didnt work anyways. It shows zero for the amount and something about senpai. Then fails to process. Ill work on it tomorrow, lighting struck near the house, going to turn everything off.

They are gamepass, not developer products, ProcessReceipt does not work with gamepass, you must use PromptGamePassPurchaseFinished

local MPS = game:GetService("MarketplaceService")

local Functs = {}
Functs[23366248] = function(player)
	player.leaderstats.Rubies.Value = player.leaderstats.Rubies.Value + 500
end
Functs[1456783] = function(player)
	player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + 500
end

MPS.PromptGamePassPurchaseFinished:Connect(function(player, Id, Purchase)
	if not Purchase then		return			end
	
	local Success, Err = pcall(Functs[Id], player)
	if not Success then			warn(Err)			end
end)

if you want them to be bought several times use developer products.

Thanks for the reply. Your script got me further but im still having problems. When I click the buy button it asks me to confirm. I confirm and it does not take any robux from my account and it does not add any Rubies. It tells me the purchase of the Rubies was successful

Any Ideas? Here is the script im using. I got rid of the game pass and used a developer product.

local MPS = game:GetService("MarketplaceService")

local Functs = {}
Functs[1211021582] = function(player)
	player.leaderstats.Rubies.Value = player.leaderstats.Rubies.Value + 500
end
--Functs[1456783] = function(player)
--	player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + 500
--end

MPS.PromptGamePassPurchaseFinished:Connect(function(player, Id, Purchase)
	if not Purchase then		return			end

	local Success, Err = pcall(Functs[Id], player)
	if not Success then			warn(Err)			end
end)

And here is the button script.

MPS = game:GetService("MarketplaceService")
id = 1211021582
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	MPS:PromptProductPurchase(player,id)
end)

I appreciate the help.