Hi everybody! You might already notice that each game has a certain gamepass and premium benefit for a certain or group of certain items.
For this topic, I will be showing you how to create a marketplace system for a game.\
Before we begin, always remember to enable the API and third party sales (game-settings>secutity):
Now let’s start off by adding a regular script in ServerScriptService and following these steps:
1. Creating the main variable
You basically have to do one thing here. Before we move on, we will have to assign our main variable.
local mps = game:GetService("MarketplaceService")
The marketplace service tab is where basically, the coding will run on. It is used for prompting purchases and checking if the player owns the gamepass or not.
2. Creating our gamepasses
Now we’re ready to move on to the products were gonna use. Let’s just add three items and do something like this:
-- Change the zeros to your gamepass ID
local Item1 = 0
local Item2 = 0
local Item3 = 0
I chose to name them Item1
, Item2
, Item3
, but you can name it whatever you want!
Now if you don’t know what’s a gamepass ID, then let me explain. Otherwise, you can move on to the next step underneath.
A gamepass ID is the ID for your gamepass. You can find it in the link to your gamepass. For example:
“VIP - Roblox” (When you click it, the ID will be the numbers inside the link address.)
3. Adding our tools
We are now at the stage where we add our tools.
To start off, find a tool you want for your gamepass or you can make one yourself. Place your tool in server storage.
Add more tools if needed in server storage. Those tools will be used for our gamepasses. Make sure to rename the tools to prevent random picking (picking a different item if the player owns the gamepass).
4. Checking if the player has the gamepass
We are now at the stage where we check is the player owns the gamepass or not. Here’s what you have to do:
- Create a function named player:
This is where we check when a player joins the game.game.Players.PlayerAdded:Connect(function(player) -- Do stuff end)
- Add your conditional statements:
This is how it looks like when we’re setting what is going to happen when a player owns the gamepass. Now this is what we’re going to do. We’re gonna take the tool we have from the server storage and hand it over to the gamepass owner. Therefore, write the code like this:if mps:UserOwnsGamePassAsync(player.UserID, Item1) then -- Do stuff end
How about doing the same thing for the other passes:if mps:UserOwnsGamePassAsync(player.UserID, Item1) then game.ServerStorage.Tool1:Clone().Parent = player:WaitForChild("Backpack") game.ServerStorage.Tool1:Clone().Parent = player:WaitForChild("StarterGear") end
So like I said, when the player joins the game with the gamepass owned, they will automatically be given the tool.if mps:UserOwnsGamePassAsync(player.UserID, Item2) then game.ServerStorage.Tool2:Clone().Parent = player:WaitForChild("Backpack") game.ServerStorage.Tool2:Clone().Parent = player:WaitForChild("StarterGear") end if mps:UserOwnsGamePassAsync(player.UserID, Item3) then game.ServerStorage.Tool3:Clone().Parent = player:WaitForChild("Backpack") game.ServerStorage.Tool3:Clone().Parent = player:WaitForChild("StarterGear") end
=============================================================================
So…voilà! You’re done, meaning your script should officially look like this in the end:
local mps = game:GetService("MarketplaceService")
-- Change the zeros to your gamepass IDs
local Item1 = 0
local Item2 = 0
local Item3 = 0
game.Players.PlayerAdded:Connect(function(player)
if mps:UserOwnsGamePassAsync(player.UserID, Item1) then
game.ServerStorage.Tool1:Clone().Parent = player:WaitForChild("Backpack")
game.ServerStorage.Tool1:Clone().Parent = player:WaitForChild("StarterGear")
end
if mps:UserOwnsGamePassAsync(player.UserID, Item2) then
game.ServerStorage.Tool2:Clone().Parent = player:WaitForChild("Backpack")
game.ServerStorage.Tool2:Clone().Parent = player:WaitForChild("StarterGear")
end
if mps:UserOwnsGamePassAsync(player.UserID, Item3) then
game.ServerStorage.Tool3:Clone().Parent = player:WaitForChild("Backpack")
game.ServerStorage.Tool3:Clone().Parent = player:WaitForChild("StarterGear")
end
end)
Extensions
In case you want to do the thing where it prompts a purchase for the pass, read this section:
You can use parts to prompt a purchase by touching or clicking them.
-
To prompt by touching, just add a script to the part, saying:
local mps = game:GetService("MarketplaceService") script.Parent.Touched:Connect(function(Hit) local player = game.Players:GetPlayerFromCharacter(Hit.Parent) mps:PromptGamePassPurchase(player, 0) end)
-
To prompt by clicking a part, you could:
- Add a click detector and a script inside the part that should say:
local gamepassID = 0 -- Remember to change the zero local mps = game:GetService("MarketplaceService") script.Parent.ClickDetector.MouseClick:Connect(function(Player) mps:PromptGamePassPurchase(Player, gamepassID) end)
OR
- Add a proximity prompt and a script inside the part that should say:
local gamepassID = 0 -- Remember to change the zero local mps = game:GetService("MarketplaceService") script.Parent.ProximityPrompt.Triggered:Connect(function(Player) mps:PromptGamePassPurchase(Player, gamepassID) end)
- To prompt with button GUIs, just add a local script in the button, saying:
local mps = game:GetService("MarketplaceService") local Player = game.Players.LocalPlayer script.Parent.MouseButton1Click:Connect(function() mps:PromptGamePassPurchase(Player, 1) end)
REMEMBER THIS: Be careful on what you set player
as. I made that mistake all the time. As a result, I kept getting an error that said, “player should be of type Player,” which meant that player
was only recognized as type “nil”.
Thanks for reading
I hope you enjoyed this topic. If you have any problems, feel free to reply anytime to this topic.
QUICK REMINDER
This is only my first tutorial I made, so if I leave anything out, feel free to reply to this topic anytime. Again, thanks for reading.
- Yes it was. Thank you!
- Nah, I already knew that!
- I knew some of it but still helpful.
0 voters