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

What's wrong with the code for my dev. product purchase handler?

Asked by 5 years ago
Edited 5 years ago

I made a script that when a dev product is purchased, and when the purchase is completed it thanks the player and adds points to a points counter in the game. Initially, to be put in a better perspective, there are 4 different game passes you can buy and what the script does is it prompts the purchase when the button in the shop gui for buying said gamepass(es) is clicked. But, when prompted, an StringValue says what amount of points is purchased (e.g. 100k points, 100 points, etc.) and then a transaction handler waits until a BoolValue, which determines if the gamepass is purchased which is activated when the purchase is complete, is true and rewards the player with the number of points determined by the StringValue. (P.S. The StringValue is not intended to be a number)

Here's the script for one of the scripts when the purchased is prompted:

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

local productID = 465963450  -- Change this to your developer product ID

-- Function to prompt purchase of the developer product
local function promptPurchase()
    local player = Players.LocalPlayer
    MarketplaceService:PromptProductPurchase(player, productID)
end

script.Parent.MouseButton1Click:Connect(function()
    promptPurchase()
    script.Parent.Parent.TransactionHandler.WhatGamepass.Value = "1k"
end)

This script prompts the player with the gamepass for 1,000 points, tells the WhatGamepass variable (the StringValue) that the player wants to buy 1,000 points. This script below handles the transaction if the (this handles all the game passes) gamepass is bought. Here's the script:

local MarketplaceService = game:GetService("MarketplaceService")
local ThxText = game.StarterGui.CoreGui.ThankingText
local Points = game.StarterGui.CoreGui.CurrencyHolder.PlayerPoints.num.Points

local function processReceipt(receiptInfo)
    -- Find the player who made the purchase in the server
    local player = game:GetService("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

    -- Output what product they bought
    script.WhatGamepass.Bought.Value = true

    -- 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

(P.S. I got this script and the other one from a Dev Wiki article about game passes and stuff) When purchased, the script tells the BoolValue Bought if the gamepass was purchased. Almost done! Then this script waits until Bought is true, then it determines what gamepass was bought which is told by the WhatGamepass variable (I cut out the other game passes, so we're using 1,000 points as an example so I won't have a ginormous eyesore of a script) Here it is:

script.Parent.Changed:Connect(function()
    if script.Parent.Value == "1k" then
        if script.Parent.Bought.Value == true then
            ThxText.Visible = true
            ThxText.Text = "Thanks for buying 1,000 Points! The points will be there forever on your account when you come back. Enjoy!"
            Points.Value = Points.Value + 1000
            wait(6)
            ThxText.Visible = false
            script.WhatGamepass.Value = nil
            script.Parent.Bought.Value = false
        end
    end
end)

Okay, now my question has been thoroughly and (somewhat) neatly edited so you hopefully won't be confused. But, as always if you are, do not hesitate to ask questions! Thanks! :)

P.S. ADMINS PLEASE DO NOT BAN THIS FOR BEING TOO LENGTHY! I REALLY NEED HELP ON THIS AND THIS IS THE ONLY SITE THAT WILL HELP ME ANSWER IT, THANK YOU!

0
i dont think you can be banned for being too length :/ theking48989987 2147 — 5y
0
Please explain your exact issue with the script(s) aswell, as the title is pretty general, and there isn't really any description of the issue in your question body theking48989987 2147 — 5y
0
You shouldn't edit values from a local script.... CaptainD_veloper 290 — 5y

1 answer

Log in to vote
0
Answered by
Zottic 19
5 years ago
Edited 5 years ago

Errors:

On your 3rd script, you said if script.Parent.Value == "1k". You can't put strings in a bool value, and don't put the ". Besides, it's not even a number. Put if script.Parent.Value == 1000. Also, what if the player didn't buy the product but had 1000, the script would print " thanks for buying 1k points" anyway.

0
I meant string value! Sorry I updated it Chez_Guy 67 — 5y
Ad

Answer this question