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

How would I enable a script after buying a developer product?

Asked by 4 years ago

Hey there, so I wanted to make a developer product that starts a warhead sequence. However, it isn't working, and there are no errors. Here's the code:

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

-- Data store for tracking purchases that were successfully processed
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")

-- Table setup containing product IDs and functions for handling purchases
local productFunctions = {}

productFunctions[965096622] = function(receipt, player)
    script.Parent.WarheadServer.Disabled = false -- the problem
        return true
    end

-- The core 'ProcessReceipt' callback function
local function processReceipt(receiptInfo)

    -- Determine if the product was already granted by checking the data store  
    local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
    local purchased = false
    local success, errorMessage = pcall(function()
        purchased = purchaseHistoryStore:GetAsync(playerProductKey)
    end)
    -- If purchase was recorded, the product was already granted
    if success and purchased then
        return Enum.ProductPurchaseDecision.PurchaseGranted
    elseif not success then
        error("Data store error:" .. errorMessage)
    end

    -- Find the player who made the purchase in the server
    local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
    if not player then
        -- The player probably left the game
        -- If they come back, the callback will be called again
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    -- Look up handler function from 'productFunctions' table above
    local handler = productFunctions[receiptInfo.ProductId]

    -- Call the handler function and catch any errors
    local success, result = pcall(handler, receiptInfo, player)
    if not success or not result then
        warn("Error occurred while processing a product purchase")
        print("\nProductId:", receiptInfo.ProductId)
        print("\nPlayer:", player)
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    -- Record transaction in data store so it isn't granted again
    local success, errorMessage = pcall(function()
        purchaseHistoryStore:SetAsync(playerProductKey, true)
    end)
    if not success then
        error("Cannot save purchase data: " .. errorMessage)
    end

    -- IMPORTANT: Tell Roblox that the game successfully handled the purchase
    return Enum.ProductPurchaseDecision.PurchaseGranted
end

-- Set the callback; this can only be done once by one script on the server! 
MarketplaceService.ProcessReceipt = processReceipt

Thanks in advance. P.S. the script is in a regular script in ServerScriptService, and so is the script I'm trying to enable.

0
What is the issue with the code? Is the script not being disabled? Or the function not being called? On a side note, I suggest using a ModuleScript instead of disabling/enabling scripts. It'll make your code much cleaner and easier to follow. climethestair 1663 — 4y
0
The script isn't being enabled, the Localscript that prompts the purchase works, and I changed it to a Modulescript. TheStarWarsMaster856 12 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

You can put the warhead script into Replicated Storage. Whenever you want to enable it, Clone the warhead script and parent it to wherever

0
Is it possible to parent the script without cloning it? TheStarWarsMaster856 12 — 3y
0
Yes, but I haven't tested if you can disable it by moving it into Rep Storage again. I recomend cloning the script, because, from my experience, scripts in rep storage don't run. Dr_Smartypants123 86 — 3y
0
Ok. TheStarWarsMaster856 12 — 3y
Ad

Answer this question