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

Purchase script doesn't give players their purchase sometimes?

Asked by 5 years ago

Sometimes this purchase script doesn't give players their items but the purchase goes through anways. I've tried having them rejoin and adding 20 second long debounces for prompting purchases when the buy button is clicked but nothing has helped."

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

local Products = {
    [433851642] = function(receipt,player)
        local stats = player:FindFirstChild("Stats")
        local gold = stats and stats:FindFirstChild("Beli")
        if not gold then return end
        gold.Value = gold.Value + 100000
        return true
    end;

    [436572678] = function(receipt,player)
        local stats = player:FindFirstChild("Stats")
        local gold = stats and stats:FindFirstChild("DevilFruit")
        if not gold then return end
        player.Stats.StatPoints.Value = player.Stats.StatPoints.Value + player.Stats.Strength.Value
        player.Stats.Strength.Value = 0
        player.Stats.StatPoints.Value = player.Stats.StatPoints.Value + player.Stats.Blade.Value
        player.Stats.Blade.Value = 0
        player.Stats.StatPoints.Value = player.Stats.StatPoints.Value + player.Stats.Defense.Value
        player.Stats.Defense.Value = 0
        player.Stats.StatPoints.Value = player.Stats.StatPoints.Value + player.Stats.StaminaLevel.Value
        player.Stats.StaminaLevel.Value = 0
        player.Stats.StatPoints.Value = player.Stats.StatPoints.Value + player.Stats.DevilFruitLevel.Value
        player.Stats.DevilFruitLevel.Value = 0
        return true
    end;
    [438066127] = function(receipt,player)
        -- DFs --
        local DFs = {"Chop","Sand","Gravity","Smoke"}
        --

        local RandomDF = math.random(1, #DFs)
        print(RandomDF)

        for i,v in pairs (DFs) do
            if RandomDF == i then
                local Storage = game:GetService("ReplicatedStorage")
                local DevilFruit = Storage.Fruits:WaitForChild(v):Clone()
                DevilFruit.Parent = player.Backpack
            end
        end
        return true
    end;

    [429585674] = function(receipt,player)
        local stats = player:FindFirstChild("Stats")
        local gold = stats and stats:FindFirstChild("Beli")
        if not gold then return end
        local Chance = math.random(1,450)
        if Chance == 450 then
        local Fruit = math.random(1,4)
        if Fruit == 1 then
        game.ReplicatedStorage.Fruits.Chop:Clone().Parent = player.Backpack
        player.Stats.XP.Value = player.Stats.XP.Value + 1000
        player.Stats.Beli.Value = player.Stats.Beli.Value + 300
        local GUI = game.ReplicatedStorage.GachaConfirm:Clone()
        GUI.Parent = player.PlayerGui
        game:GetService("Debris"):AddItem(GUI, 5)
    elseif Fruit == 2 then
        game.ReplicatedStorage.Fruits.Smoke:Clone().Parent = player.Backpack
        player.Stats.XP.Value = player.Stats.XP.Value + 1000
        player.Stats.Beli.Value = player.Stats.Beli.Value + 300
        local GUI = game.ReplicatedStorage.GachaConfirm:Clone()
        GUI.Parent = player.PlayerGui
        game:GetService("Debris"):AddItem(GUI, 5)
    elseif Fruit == 3 then
        game.ReplicatedStorage.Fruits.Gravity:Clone().Parent = player.Backpack
        player.Stats.XP.Value = player.Stats.XP.Value + 1000
        player.Stats.Beli.Value = player.Stats.Beli.Value + 300
        local GUI = game.ReplicatedStorage.GachaConfirm:Clone()
        GUI.Parent = player.PlayerGui
        game:GetService("Debris"):AddItem(GUI, 5)
    elseif Fruit == 4 then
        game.ReplicatedStorage.Fruits.Sand:Clone().Parent = player.Backpack
        player.Stats.XP.Value = player.Stats.XP.Value + 1000
        player.Stats.Beli.Value = player.Stats.Beli.Value + 300
        local GUI = game.ReplicatedStorage.GachaConfirm:Clone()
        GUI.Parent = player.PlayerGui
        game:GetService("Debris"):AddItem(GUI, 5)
        end
    elseif Chance ~= 450 then
        player.Stats.XP.Value = player.Stats.XP.Value + 1000
        player.Stats.Beli.Value = player.Stats.Beli.Value + 300
        local GUI = game.ReplicatedStorage.GachaConfirm:Clone()
        GUI.Parent = player.PlayerGui
        game:GetService("Debris"):AddItem(GUI, 5)
    end 
        return true
    end;



}

function MarketplaceService.ProcessReceipt(receiptInfo)
    local playerProductKey = receiptInfo.PlayerId .. ":" .. receiptInfo.PurchaseId
    if PurchaseHistory:GetAsync(playerProductKey) then
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

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


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


    if not handler then return Enum.ProductPurchaseDecision.PurchaseGranted end


    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)
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end


    if not err then
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end


    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)
        print("\t Handler worked fine, purchase granted")
    end

    return Enum.ProductPurchaseDecision.PurchaseGranted
end

Some help would be greatly appreciated, thanks!

0
Umm, yeah that's kinda hard to read. I have a script for another devproduct if you want it? CaptainD_veloper 290 — 5y
0
what are thooooose DinozCreates 1070 — 5y

Answer this question