Why is my devProducts Script not working?

I have many devProducts, and I have put all their scripts, but when I buy the devProduct any of them, purchase is successful but my character doesn’t get the item.

What’s the error in this script?

local MarketplaceService = game:GetService('MarketplaceService')
local HTTP = game:GetService("HttpService")

MarketplaceService.ProcessReceipt = function(receiptInfo)
	for i, Player in ipairs(game.Players:GetChildren()) do
		if receiptInfo.ProductId == 965758602 then 
				local Tool = game.ServerStorage["Carpet"]:Clone()
				Tool.Parent = Player.Backpack
		elseif receiptInfo.ProductId == 965759324 then 
			local Tool = game.ServerStorage["GravityCoil"]:Clone()
			Tool.Parent = Player.Backpack
			elseif receiptInfo.ProductId == 965759193 then 
			local Tool = game.ServerStorage["BBGun"]:Clone()
			Tool.Parent = Player.Backpack
			elseif receiptInfo.ProductId == 965759481 then 
			local Tool = game.ServerStorage["BodySwap"]:Clone()
			Tool.Parent = Player.Backpack
			elseif receiptInfo.ProductId == 965759590 then 
			local Tool = game.ServerStorage["BoomBox"]:Clone()
			Tool.Parent = Player.Backpack
			elseif receiptInfo.ProductId == 965759769 then 
			local Tool = game.ServerStorage["GrappleHook"]:Clone()
			Tool.Parent = Player.Backpack
			elseif receiptInfo.ProductId == 965759961 then 
			local Tool = game.ServerStorage["HyperlaserGun"]:Clone()
			Tool.Parent = Player.Backpack
			elseif receiptInfo.ProductId == 965760079 then 
			local Tool = game.ServerStorage["RainbowSword"]:Clone()
			Tool.Parent = Player.Backpack
			elseif receiptInfo.ProductId == 965760204 then 
			local Tool = game.ServerStorage["SpeedCoil"]:Clone()
			Tool.Parent = Player.Backpack
			elseif receiptInfo.ProductId == 965760325 then 
			local Tool = game.ServerStorage["RoPhone XS"]:Clone()
			Tool.Parent = Player.Backpack
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	end
end

It is best to deal with purchases on the client and then send that info to the server for the server to process it and give the respected item to the player

What could be causing the error in your script, well its kind of hard to tell. In order to isolate where the error could be coming from trying using print statements after each if statement to see if it has actually ran through that if statement.

Not when it comes to developer products. The only thing that the client should be responsible for handling is prompting the product purchase and any interface updates based on what option the user selects (either a successful purchase or a cancellation). Products must be handled by the server.

Part of the problem is that this code is so messy and can be crunched down and made scalable by a large margin. This script iterates over every player (and it’s not done properly either; use GetPlayers, not GetChildren) and if someone purchased a certain product, everyone gets items.

This can be fixed with a dictionary where the key is a ProductId and the value is a tool to be granted, not iterating over every player per purchase and handling purchases properly. There are various code samples that can help with determining the correct way to do all of this.

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ServerStorage = game:GetService("ServerStorage")

local PRODUCTS = {
    [965758602] = "Carpet", -- Only one for example's sake
}

function MarketplaceService.ProcessReceipt(receiptInfo)
    local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
    if not player then
        -- You MUST queue purchases for players if it can't be granted at the time
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    local productTool = PRODUCTS[receiptInfo.ProductId]
    if productTool then
        ServerStorage[productTool]:Clone().Parent = player.Backpack
    else
        -- Assume all products must grant a tool, otherwise prevent processing of purchases
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    return Enum.ProductPurchaseDecision.PurchaseGranted
end

Honestly though it would help if OP provided more details about their circumstances and did their own debugging, as well as read some reference articles for help first before posting a vague thread. It’s always important to consult documentation first as that can help show where a potential error might be in handling things, even for fundamental flaws. ProcessReceipt article.

3 Likes

Thanks Colbert, however it did work, my Skip Stage Script isn’t working now, I guess I will have to make a seperate topic on that.