Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How could I fix this broken on buy give tool Dev Product script?

So this devproduct script is supposed to give a tool to the player if he buys the dev product. It doesn't want to work sometimes. When I try it, it does nothing.. so here it is:

local devProductId = 48808572 
local pl = script.Parent.Parent.Parent.Parent.Parent


script.Parent.MouseButton1Click:connect(function()
	game:GetService("MarketplaceService"):PromptProductPurchase(pl, devProductId)
end)

local MarketplaceService = game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
MarketplaceService.ProcessReceipt = function(receiptInfo)
	for i, player in ipairs(game.Players:GetChildren()) do
		if player.userId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == devProductId then
				local P = game.Players:FindFirstChild(player.Name)
				local miniguncopy = game.ServerStorage.MiniGun:Clone()				
				miniguncopy.Parent = P.Backpack				
			end
		end
	end
	local playerProductKey = "pl_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.ProductId
		ds:IncrementAsync(playerProductKey, 1)	
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

1 answer

✓ Accepted answer

Is this a Local Script or Server Script. If the code is in a Local Script it would crash because ServerStorage can be accessed by Server Scripts only! I would recommend you separate Line 1 to 7 (in a Local Script) and Line 9 to 24 (Server Script). It is better because in the Local Script, you can use LocalPlayer as your player instead of using lots of "Parents". The process receipt will be fired once the player makes the purchase!

In a Local Script in the button:

local devProductId = 48808572 
local pl = game.Players.LocalPlayer


script.Parent.MouseButton1Click:Connect(function() -- connect is deprecated, change to the one with capital C!
    game:GetService("MarketplaceService"):PromptProductPurchase(pl, devProductId)
end)

In a Server Script in ServerScriptService:

local devProductId = 48808572  -- Add this to your script to check for the product.
local MarketplaceService = game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
MarketplaceService.ProcessReceipt = function(receiptInfo)
    for i, player in ipairs(game.Players:GetChildren()) do
        if player.UserId == receiptInfo.PlayerId then -- userId is deprecated, use UserId instead
            if receiptInfo.ProductId == devProductId then
                local P = game.Players:FindFirstChild(player.Name)
                local miniguncopy = game.ServerStorage.MiniGun:Clone()              
                miniguncopy.Parent = P.Backpack             
            end
        end
    end
    local playerProductKey = "pl_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.ProductId
        ds:IncrementAsync(playerProductKey, 1)  
    return Enum.ProductPurchaseDecision.PurchaseGranted
end

Also, I see you used some deprecated terms in your script. Make sure to use the ones that are up-to-date! Look at the wiki for more information!

If you have any questions or additional errors, please leave a comment below. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.