Studio Model Purchases In-Game

So A long time ago I made a script that makes it to where when you click a button it prompts the sale of a model. Today I want to explain and Open-Source it.

The script is good for something like my game Kit Hub where it is used to display kits after the button is clicked.

How Does It Work?

basically it is an AssetPrompt Button (Doesn’t matter if you add UI or not, ignore the other stuff, just focus on the “AssetPrompt” part. The AssetPrompt button is then clicked and the model of choosing is displayed (Please Note Third-Party Sales must be enabled for it to prompt any items not made by your or your group).

How To Script It!

Start by adding a few things,

  1. A Normal Part
  2. A Click Detector Within that part
  3. A Script within That Click Detector (NORMAL NOT LOCAL)
  4. Optional UI (Within the Part)

Next, Open the script and delete the default starting code.

Now, lets start.

We need to first call some things we will use later in the script. Your script should look like this.

local ASSET_ID = 123456 -- ID of the marketplace/library asset you want to give players.
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsAsset = MarketplaceService.PlayerOwnsAsset

Now lets add the function that will give players the asset, click enter two times and add

script.Parent.MouseClick:Connect(function(player)

What’s inside? Its code! Lets start writing the inside the function.

	local success, HaveAsset = pcall(PlayerOwnsAsset, MarketplaceService, player, ASSET_ID)
	if (success and HaveAsset) then
		print("Have Asset")

If player’s have the asset that prompts it, but what if they don’t?

Lets add an else to make sure the code doesn’t error

	else
		game:GetService("MarketplaceService"):PromptPurchase(player, 123456) -- Asset ID Here also

Now top it off with some ends and bam it should work!

	end
end)

Final Coding:

The finalized script should look like this:

local ASSET_ID = 123456 -- Asset ID
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsAsset = MarketplaceService.PlayerOwnsAsset

script.Parent.MouseClick:Connect(function(player)
	local success, HaveAsset = pcall(PlayerOwnsAsset, MarketplaceService, player, ASSET_ID)
	if (success and HaveAsset) then
		print("Have Asset")
	else
		game:GetService("MarketplaceService"):PromptPurchase(player, 123456) -- Asset ID
	end
end)

Link To Model:

Here is a link to the model if you want to skip the tutorial

Hope this helped, please tell me how you use it if you do, I would love to hear!

4 Likes

Could you link the model in the thread?

This seems to be more of a tutorial. Would you mind updating the category?

Thanks

1 Like

Sure thing, let me get the model up really quick!

Edit: @HDAdminUnofficial I added the model link at the end of the tutorial

2 Likes

The code above will always print “Have Asset”, pcall returns the error message if success is false.

You should fix this by checking that success is also true:

if (success and HaveAsset) then

P.S: the correct grammar would be “has asset”, not “have asset”. :ok_hand:

2 Likes

Oh okay, didn’t realize that was the case!

1 Like

Well made! I could see this getting used in some games.

1 Like