Earning Robux - Part 2

Earning Robux - Part 2

  • General knowledge of Studio covered in Intro to Studio
  • Knowledge of scripting covered in Coding 2
  • Students should have completed Adventure Game - Earning Robux Part 1
  • Lesson Time 10 - 20 minutes
    Optional Handouts
  • Game Reference Document
  • Learning Objectives
  • Learn how to use premade scripts to make an in-game purchase
  • Understand how to activate a trail effect when a player makes a game pass purchase
  • 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.

    1. Go back into your project in Studio.
    2. Right-click on Shop and select Duplicate to create a new version of the shop.
    1. Rename the duplicated Shop to GamePassShop. Under BuyButton, rename BuyScript to be BuyGamePass.
    1. In BuyButton > SurfaceGUI > BuyText, change the text to something more descriptive for your game pass.
    2. 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.

    1. In the Explorer, in ReplicatedStorage, add a new ModuleScript.
    1. Rename the ModuleScript HasGamepassChecker.
    Spell the Script Name Exactly

    If HasGamepassChecker is spelled or capitalized differently, the game pass scripts will not work.

    1. 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.
    1. In the script HasGamepassChecker, delete the Hello World line and paste the copied script.
    2. 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.

    1. Left click on the arrow next to StarterPlayer.
    2. In StarterPlayerScripts, add a new LocalScript. A LocalScript only runs for the player. Not the whole server.
    1. Rename the local script to GamePassNotify and delete Hello World.
    1. Copy the entire script below:
    1. 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.

    1. In the Explorer, under GamePassShop, open BuyPassScript.
    2. Delete all of the existing code, so it can be replaced with updated code.
    3. 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)
    1. To work with your game pass, the placeholder game pass ID needs to be replaced with your own ID. On Line 6, replace 0000000 with the game pass ID you wrote down.
    1. Play your game and click on the button to buy the game pass.
    If you playtest, you'll see that even if you own the game pass, it doesn't display the trail on your tool yet. You'll create a script in the next section so the game pass turns on the tool's trail.

    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.

    • 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.

    1. Under StarterPack in your Tool, add a new script named TrailEffectScript.
    1. Copy and paste the code below into TrailEffectScript. This script will run a function named enableGamePassEffect that 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
    --================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")
    	print("player either doesn't have pass or there was an error and player didn't want to retry")
    -- 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")
    		print("player either doesn't have pass or there was an error and player didn't want to retry")
    1. In line 2, change 00000000 to your game pass ID.
    2. In the function enableGamePassEffect, type the following to get the Trail attached to that tool.
    local function enableGamePassEffect()
        local trailEffect = tool:FindFirstChild("Trail")
    1. 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
    1. 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.