Earning Robux - Part 2
Earning Robux - Part 2
Creating an In-Game Purchase
Now that a game pass is created through the Roblox website, players need someway to buy it in your game. This project will have a physical shop, just like how players bought upgrades to their cupcake bag. If a player clicks on a button, they’ll get an option to buy your game pass.
Set Up a In-Game Shop
To create a buy button for the game pass, duplicate the current shop and just change the script for it.
- Go back into your project in Studio.
- Right-click on Shop and select Duplicate to create a new version of the shop.
- Rename the duplicated Shop to GamePassShop. Under BuyButton, rename BuyScript to be BuyGamePass.
- In BuyButton > SurfaceGUI > BuyText, change the text to something more descriptive for your game pass.
- Move the GamePassShop somewhere near your original Shop.
Add HasGamepassChecker Script
In order to make game pass purchases, the game needs scripts that will take care of making purchases and seeing if players already have game passes. You’ll copy and paste premade scripts into your game.
The first script will check if players already have the game pass using a ModuleScript. This is a type of script that holds code other scripts can use.
- In the Explorer, in ReplicatedStorage, add a new ModuleScript.
- Rename the ModuleScript HasGamepassChecker.
Spell the Script Name Exactly
If HasGamepassChecker is spelled or capitalized differently, the game pass scripts will not work.
- Press the Copy button to copy the entire script below. This script will talk to the Roblox servers, and find out if that player has a specific game pass.
- In the script HasGamepassChecker, delete the Hello World line and paste the copied script.
- In the same script, right-click anywhere in the script editor and select Paste.
Add GamePassNotify Script
Since Roblox is played online, it’s possible to have some issues connecting to the internet. This can be frustrating if it causes players to not see a game pass purchase in-game. This script will let players know if there’s any issues looking for the game pass.
- Left click on the arrow next to StarterPlayer.
- In StarterPlayerScripts, add a new LocalScript. A LocalScript only runs for the player. Not the whole server.
- Rename the local script to GamePassNotify and delete Hello World.
- Copy the entire script below:
- In GamePassNotfiy, right-click anywhere in the script editor and select Paste.
Set Up a Purchase Script
The shop should ask players if they want to make a purchase whenever they click the button. Use the code below.
- In the Explorer, under GamePassShop, open BuyPassScript.
- Delete all of the existing code, so it can be replaced with updated code.
- Copy and paste the code below into the script.
-- Gets the click detector on the object buyButton = script.Parent clickDetector = buyButton.ClickDetector local MarketplaceService = game:GetService("MarketplaceService") local gamePassID = 0000000 -- Change this to your game pass ID -- Whenever someone presses the click detector, run this code local function buyPass(player) -- Ask players to buy a specific game pass based on it's ID MarketplaceService:PromptGamePassPurchase(player, gamePassID) end clickDetector.MouseClick:Connect(buyPass)
- To work with your game pass, the placeholder game pass ID needs to be replaced with your own ID. On Line 6, replace
0000000with the game pass ID you wrote down.
- Play your game and click on the button to buy the game pass.
You Won't Be Charged for This Purchase in Studio
Don’t worry, your account won’t be charged for making this test purchase. If you or your friends play the published game online, they will be charged.
Game Pass not Working? »
- Purchase Failed: If you got a message “Your purchase failed because something was wrong”, check the gamePassID in the BuyGamepass script. The number for gamePassID needs to be the same as the number found in your web browser’s address bar.
- Game Pass Not For Sale: Go back to where you created the game pass, configure it, and make sure “Item for Sale” is turned on.
- Output Window Errors: Make sure the scripts HasGamepassChecker and GamePassNotify are spelled exactly as seen in the lesson. If not, you may get errors as your script try to find them.
- Game Pass Already Owned: You need to delete the game pass from your Inventory. Go to roblox.com/develop. On the left hand side, go to Game Passes. Find your pass, click on the […] and select Delete.
Give Players the Trail Effect
Since the trail effect on the tool is turned off by default, you’ll need a script to enable it whenever a player has the game pass. Once the player buys the game pass, they’ll always have this effect even if they rejoin the game.
- Under StarterPack in your Tool, add a new script named TrailEffectScript.
- Copy and paste the code below into TrailEffectScript. This script will run a function named
enableGamePassEffectthat checks if players have a specific game pass.
-- NOTE: Change this to the Game Pass ID found in your web browser local GAMEPASS_ID = 4989126 local tool = script:FindFirstAncestorWhichIsA("Tool") local player = tool:FindFirstAncestorWhichIsA("Player") -- Gives the player an effect whenever they buy that game pass local function enableGamePassEffect() -- Insert effects for the game pass here end --================DO NOT EDIT UNDER HERE =============================== local ReplicatedStorage = game:GetService("ReplicatedStorage") local HasGamepassChecker = require(ReplicatedStorage:FindFirstChild("HasGamepassChecker")) local MarketplaceService = game:GetService("MarketplaceService") -- Checks if the player has the game pass when the game starts. if HasGamepassChecker:getPlayerHasPass(player.UserId, GAMEPASS_ID) then print("player has pass") enableGamePassEffect() else print("player either doesn't have pass or there was an error and player didn't want to retry") end -- Checks if the player purchases the game pass during the game local function onPromptGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess) if purchaseSuccess == true and purchasedPassID == GAMEPASS_ID then print("player has pass") enableGamePassEffect() else print("player either doesn't have pass or there was an error and player didn't want to retry") end end MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)
- In line 2, change
00000000to your game pass ID.
- In the function
enableGamePassEffect, type the following to get the Trail attached to that tool.
local function enableGamePassEffect() local trailEffect = tool:FindFirstChild("Trail") end
- To turn on the trail, the Enabled property of the trail has to be true. On the next line, type
trailEffect.Enabled = true. This turns on the trail effect.
local function enableGamePassEffect() local trailEffect = tool:FindFirstChild("Trail") trailEffect.Enabled = true end
- Play your game and test that the trail works after buying the game pass.
These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.