Why my script not working

local MPS = game:GetService("MarketplaceService")

-- my Product
local PRODUCTS = {
	["CASH_100"]   = 3467587037, -- 100 Cash
	["CASH_1100"]  = 3467587678, -- 1100 Cash
	["CASH_5500"]  = 3467588262, -- 5500 Cash

	["DIA_5"]      = 3467589018, -- 5 Diamond
	["DIA_55"]     = 3467589697, -- 55 Diamond
	["DIA_250"]    = 3467590382, -- 250 Diamond
}


local REWARDS = {
	[PRODUCTS["CASH_100"]]  = {type = "Cash", amount = 100},
	[PRODUCTS["CASH_1100"]] = {type = "Cash", amount = 1100},
	[PRODUCTS["CASH_5500"]] = {type = "Cash", amount = 5500},

	[PRODUCTS["DIA_5"]]     = {type = "Diamond", amount = 5},
	[PRODUCTS["DIA_55"]]    = {type = "Diamond", amount = 55},
	[PRODUCTS["DIA_250"]]   = {type = "Diamond", amount = 250},
}

local function processReceipt(receiptInfo)
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local reward = REWARDS[receiptInfo.ProductId]
	if reward then
		local stats = player:FindFirstChild("leaderstats")
		if stats then
			local currency = stats:FindFirstChild(reward.type)
			if currency then
				currency.Value = currency.Value + reward.amount
			end
		end
	end

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MPS.ProcessReceipt = processReceipt

5 Likes

Try to do a print(player) above the if not player then statement and check if it prints the actual player or nil. Additionally check the console for any errors relating to this script and function.

2 Likes

Have you published your game to Roblox yet?

2 Likes

Hey, we need to better understand where the function for processing receipt breaks. Try adding print() statements across the function like so:

local function processReceipt(receiptInfo)
	print("PROCESSING RECEIPT:", receiptInfo.PlayerId, receiptInfo.ProductId)

	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)

	if not player then
		warn("Player not found:", receiptInfo.PlayerId)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local reward = REWARDS[receiptInfo.ProductId]

	if not reward then
		warn("UNKNOWN PRODUCT ID:", receiptInfo.ProductId)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local stats = player:FindFirstChild("leaderstats")

	if not stats then
		warn("leaderstats missing for", player.Name)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local currency = stats:FindFirstChild(reward.type)

	if not currency then
		warn("Currency missing:", reward.type)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	currency.Value += reward.amount

	print("GRANTED:", reward.type, reward.amount, "to", player.Name)

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

Then we will know exactly where to start fixing it. (make sure to share output in forum)

2 Likes

Could you also share the LocalScript which handles the purchase prompt system? I’m pretty sure the issue is that it is specified anything else except “PromptProductPurchase”, as i guess this is dev product, not a gamepass, avatar item, etc. Also make sure the ID if of the purchase is correct.

1 Like

Of course, the game is private, could this be the problem?
After all, I don’t think it has a local code because the server is going to pay the player.

No it’s not a problem with the game being private.

local button = script.Parent
local player = game.Players.LocalPlayer
local gamePassId = script.Parent.GamepassId

button.MouseButton1Click:connect(function()
	if player then
		game:GetService("MarketplaceService"):PromptGamePassPurchase(player, gamePassId.Value)
	end
end)

Make sure the GamepassId.Value is the correct ID, and that it is a number not a string

1 Like

Checking if the player exist inside MouseButton1Click is not necessary

could you show what is inside GamepassId.Value?
The error is probably happening because script cannot find script.Parent.GamepassId or because it contains invalid gamepass ID inside.

Can you please make a variable for the MarketPlaceService because it’s a bad practice to keep calling GetService. Also, can you try to directly put the id in the script?

1 Like

try doing:

-- services
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

-- variables
local button = script.Parent
local player = Players.LocalPlayer
local gamePassId = 123 -- Put your actual ID here

button.MouseButton1Click:Connect(function()
    local success, err = pcall(function() 
        MarketplaceService:PromptGamePassPurchase(player, gamePassId)
    end)

    if not success then
        warn("Failed to prompt purchase, error: \n"..err)
    end
end)

As @099ersf2 said it is bad practice using game:GetService inside things like events, because it means Roblox will try to get already existing service every time the event happens, by putting it on top we use same one every time. And yes, player check is not necessary.

If it still does not work, try making another gamepass and using new id… if still not working then there is research that will need to be done.

2 Likes

I forgot about pcalls. Good catch. It may help figure out what’s really happening.

2 Likes