Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Trying to make a developer product purchase script but it only works in roblox studio play mode?

Asked by 6 years ago
Edited 6 years ago

okay so this code is taken from the roblox wiki and only works in roblox test mode as in it outputs that the purchase was successful and gives me the $1000 but it does not do this in the actual game my game is filtering enabled but i dont know why its not working?

local MarketplaceService = game:GetService("MarketplaceService")
local PurchaseHistory = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")

--[[
    This is how the table below has to be set up:
        [productId] = function(receipt,player) return allow end
            receipt is the receiptInfo, as called by ProcessReceipt
            player is the player that is doing the purchase
        If your function returns 'true', the purchase is approved.
        If your function doesn't return 'true', or errors, the purchase is cancelled.
--]]
local Products = {
    -- productId 2222 for 100 gold
    [331858807] = function(receipt,player)
        -- again, do checks, this time for the Gold value
        local stats = player.PlayerGui:FindFirstChild("Money")
        local gold = stats.backdrop:FindFirstChild("money")
        if not gold then return end -- no leaderstats, or "Gold" in them
        gold.Value = gold.Value + 1000 -- give gold
        return true -- tell them of our success
    end;
}

-- set MarketplaceService.ProcessReceipt to this function
-- (this is the same as doing: MarketplaceService.ProcessReceipt = function(recei... )
function MarketplaceService.ProcessReceipt(receiptInfo) 
    local playerProductKey = receiptInfo.PlayerId .. ":" .. receiptInfo.PurchaseId
    if PurchaseHistory:GetAsync(playerProductKey) then
        return Enum.ProductPurchaseDecision.PurchaseGranted --We already granted it.
    end
    -- find the player based on the PlayerId in receiptInfo
    local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
    if not player then return Enum.ProductPurchaseDecision.NotProcessedYet end
    -- player left? don't process it

    local handler
    for productId,func in pairs(Products) do
        if productId == receiptInfo.ProductId then
            handler = func break -- found our handler
        end
    end

    -- apparently it's not our responsibility to handle this purchase
    -- if this happens, you should probably check your productIds etc
    -- let's just assume this is ment behavior, and let the purchase go through
    if not handler then return Enum.ProductPurchaseDecision.PurchaseGranted end

    -- call it safely with pcall, to catch any error
    local suc,err = pcall(handler,receiptInfo,player)
    if not suc then
        warn("An error occured while processing a product purchase")
        print("\t ProductId:",receiptInfo.ProductId)
        print("\t Player:",player)
        print("\t Error message:",err) -- log it to the output
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    -- if the function didn't error, 'err' will be whatever the function returned
    -- if our handler didn't return anything (or it returned false/nil), it means
    -- that the purchase failed for some reason, so we have to cancel it
    if not err then
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    -- record the transaction in a Data Store
    suc,err = pcall(function()
        PurchaseHistory:SetAsync(playerProductKey, true)
    end)
    if not suc then
        print("An error occured while saving a product purchase")
        print("\t ProductId:",receiptInfo.ProductId)
        print("\t Player:",player)
        print("\t Error message:",err) -- log it to the output
        print("\t Handler worked fine, purchase granted") -- add a small note that the actual purchase has succeed
    end
    -- tell Roblox that we have successfully handled the transaction (required)
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end
0
Try to check ingame the developer console maybe you can get something from it SulaymanArafat 230 — 6y
0
Is thes a script or a local script? User#5423 17 — 6y
0
this is a normal script IwanCodes 8 — 6y
0
Do you get any errors when testing it? Where does the script go wrong? chomboghai 2044 — 6y

Answer this question