How to make a button to confirm item purchase from marketplace service id input?

Hey guys, so let me firstly introduce you to what I’ve made:

When you write an id of an item inside the catalog it will prompt to purchase it inside the game when you press, “Enter”.

However, I’m wanting to make a button so that when you paste in the id of the item, then you press the buy button to prompt the purchase that was written inside the id input box, if there was no id input, it would say “Please enter the ID of the item in the box first” inside a text label below, how would I do this, I’m not too sure on how to communicate with the text box and the button.

Thanks, if anything confuses you - please do ask for a better explanation.

Hm, alright if the ID Box is a TextBox we can detect changes from that and send a purchase if the ID is valid:

First we’ll go ahead and define our variables at the start of our script, so that we can easily reach them whenever we need em

MarketplaceService is how we’ll handle the product purchase, and to do that we can use a couple of Events which will fire whenever the UI Objects get interacted, we’ll probably use:

  • TextButton.MouseButton1Down (Will fire when the button is held down & up)

  • TextBox.FocusLost (Will fire when the player’s mouse stops typing)

I believe we can also check if the player has entered a valid Catalog ID or not by encasing it in a pcall (Which is basically a protective call)

local Player = game.Players.LocalPlayer
local MarketService = game:GetService("MarketplaceService")
local IDBox = script.Parent.IDBox
local ConfirmPurchase = script.Parent.ConfirmPurchase
local TextConfirm = script.Parent.TextLabel
local ID

IDBox.FocusLost:Connect(function()
    ID = tonumber(IDBox.Text)
end)

ConfirmPurchase.MouseButton1Down:Connect(function()
    if ID ~= nil then
        local SuccessCheck, NotValid = pcall(function()
            MarketService:PromptPurchase(Player, ID)
        end)

        if SuccessCheck then
            print(Player.Name.." has inserted a valid Catalog ID")
        else
            warn("That's not a valid Catalog ID!")
        end
    else
        TextConfirm.Text = "Please enter the ID of the item in the box first"
        wait(3)
        TextConfirm.Text = "Press ENTER to confirm"
    end
end)

This is just an example though, but feel free to experiment around & do ask any questions if you have any

1 Like

Awesome dude, appreciate the support, worked without a problem!

1 Like

Oh yes before I forget you would also need to include a PromptPurchaseFinished Event as well to detect if the player has bought it or not

Example:

MarketService.PromptProductFinished:Connect(function(Player, ID, PurchaseCheck)
    if PurchaseCheck == true then
        print(Player.Name.." has purchased a ID!")
        --Do stuff here
    end
end)
1 Like

Works once again as always - thank you. However do you know why there is a 3-4 second delay between when it detects the purchase/failed purchase or is that just a roblox thing?

I believe that’s just ROBLOX’s Servers detecting the process between the Catalog & inside Studio, (Either that or the player hits the “Ok” button after purchasing) it shouldn’t affect too much though hopefully

1 Like

Alrighty, well I really do I appreciate your support - without you I’d be really stuck. Have a great day Jack, cya! :slight_smile:

1 Like