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

[REPOST]Coins dev product wont work?

I have Two scripts that are suppose to make it so when you click a button it will ask if you want to buy something(with robux), and and when you click to buy it it will add to a leader stat(for the person who bought it).

this script is in ServerScriptService:

local MarketplaceService = game:GetService('MarketplaceService')
local devProductid = 391839362
MarketplaceService.ProcessReceipt = function(receiptInfo)
    for i, player in pairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value +100 
        end
    end
    return Enum.ProductPurchaseDecision.PurchaseGranted
end

This is in the text button:

local devproductid = 391839362
local button = script.Parent
button.MouseButton1Click:Connect(function()
    game:GetService('MarketplaceService'):PromptProductPurchase(game.Players.LocalPlayer, devproductid)
end)

1 answer

Remember to catch all players with :GetPlayers() no :GetChildren() with GetChildren can catch any instance other than a player.

If you want to see more of dev product see here: Roblox Wiki - Dev Products

For get the player you can use the :GetPlayerByUserId(PlayerID) and for get the id you can use productInfo.PlayerId. You can see of :GetPlayerByUserId here: Roblox Wiki - GetPlayerByUserId

You need to detect if player exists if not you need to return Enum.ProductPurchaseDurch.NotProcessedYet.

Example:

MarketplaceService.ProcessReceipt = function(receiptInfo)
    local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId) -- Get player
    if player then -- If find the player start function.
        warn("Player in game..")
    else
        warn("Not get the player!")
        return Enum.ProductPurchaseDecision.NotProcessedYet -- Stop the Purchase.
    end
end

Here is fixed script:

local MarketplaceService = game:GetService("MarketplaceService")
local devProductid = 391839362

MarketplaceService.ProcessReceipt = function(receiptInfo)
    local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
    if player then
        if receiptInfo.ProductId == devProductid then
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 100 
			return Enum.ProductPurchaseDecision.PurchaseGranted
        end
    else
        warn("Not get the player!")
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end
end

Hope it helped! :)

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