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,
- A Normal Part
- A Click Detector Within that part
- A Script within That Click Detector (NORMAL NOT LOCAL)
- 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!