PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Upgrades

Upgrades

Prerequisites
  • General knowledge of Studio covered in Intro to Studio
  • Knowledge of scripting covered in Coding 2
  • Students should have completed Adventure Game - Selling Items
  • Lesson Time 10 - 20 minutes
    Optional Handouts
  • Game Vision Handout
  • Game Reference Document
  • Learning Objectives
  • Use Surface GUIs to apply text onto a part to display information
  • Create a ClickDetector to check if users click on a part to buy an upgrade
  • This brings us to the last stage of the game loop - buying upgrades. By letting players buy upgrades that increase the size of their cupcake bag, they can harvest more cupcakes per trip and earn even more gold.

    Creating a Shop

    Each shop will have a button that players click to purchase a larger cupcake bag.

    Set Up the Shop

    1. In the Workspace, create a new model named Shop.
    2. In Shop, create a new block part named BuyButton.

    Add a Surface GUI Sign

    You’ll use a Surface GUI to add text to the part so players know what they’re buying.

    1. In BuyButton, add a new Surface GUI by clicking the + and scrolling to GUI.
    1. In Surface GUI, add a new TextLabel named BuyText. A small label will appear somewhere on the part.

    Depending on how the part was made, the label can be somewhere else. If you don’t see the text on the side you want:

    1. In the Properties for Surface GUI, scroll-down to Data.
    2. Change the Face property until the text label appears the way you want. This may be different for each part.


    Change the Surface GUI Text

    Right now, the TextLabel is really small and hard for players to see. It needs to be scaled up.

    1. In the BuyText properties, click on the arrow next to Size.
    1. Change offset for X (left and right) and Y (up and down) to 0.
    1. Change the scale of X and Y to 0.5 to make a square.
    1. Scroll up in the TextLabel properties and click the arrow left of AnchorPoint. Type in 0.5 for X and Y. This moves part of your label out of view but you’ll position it correctly next.
    1. Scroll-down in the properties and open Position. Change the scale of X and Y to 0.5 so the box is centered in the middle.
    1. In the BuyText properties, scroll-down and change Text to something descriptive. This example includes the name, cost (100), and how many spaces it’ll give players (10).
    1. Check TextScaled to be on. This automatically scales your text so it fits the box.

    Add a Click Detector

    Players will buy items by clicking on the shop rather than just touching it. The script will use a Click Detector to tell if a player has clicked the shop sign.

    Other Uses for Click Detectors

    Click Detectors can be used to activate switches on doors, give the player points, or add different types of interactivity in games.


    1. In the BuyButton, add a ClickDetector.
    1. In BuyButton, add a new script named BuyScript and give it a descriptive comment.
    1. In BuyScript, type:
    -- Lets players click a button to buy an upgrade that increases Spaces
    local buyButton = script.Parent 
    local clickDetector = buyButton.ClickDetector 
    
    1. Create a new function named giveUpgrade that gets a parameter named player. The function will upgrade a player’s spaces whenever they click the button.
    local buyButton = script.Parent
    local clickDetector = buyButton.ClickDetector
    
    local function giveUpgrade(player)
    
    end
    
    1. After the function, type clickDetector.MouseClick:Connect(giveUpgrade). Whenever players click on the part, this line will call giveUpgrade.
    local function giveUpgrade(player)
    
    end
    
    clickDetector.MouseClick:Connect(giveUpgrade)
    
    1. Add a print statement in giveUpgrade, and then test the function.
    local function giveUpgrade(player)
    	print("Someone clicked the button")
    end
    
    1. Play your project. Click the button and check that you see the text in the Output Window.
    Button Not Working?
    • In the Explorer, check that the ClickDetector is a child of BuyButton.
    • Make sure you unequip your tool before clicking the button.

    Finish the Shop

    Right now, players might not know they can’t click on the shop without hiding tools. Also, it’s possible to click the shop button from far away, which wouldn’t feel natural to players who assume they need to walk up to a shop to use it. To finish the shop, you’ll fix these two issues so the shop to make the shop easier to use.

    1. So players know how your shop works, use what you learned about Surface GUI to add a text label telling players to hide their tools.
    1. To change how far players can click the button, in Shop > BuyButton > ClickDetector change the MaxActiviationDistance to 5. Now, players will have to walk up to a button to click it.

    Buying Upgrades

    With a working button, it’s time to add code into giveUpgrade to remove a player’s gold in exchange for an upgraded bag.

    Add Variables for the Upgrade

    Each upgrade will have two variables: the cost of the upgrade and how many spaces it has.

    1. In BuyScript, under local clickDetector, type local newSpaces = 10. This will be the total spaces a player has after the purchase.
    local clickDetector = buyButton.ClickDetector
    
    -- Variables for the upgrade
    local newSpaces = 10
    
    1. On a new line, type local upgradeCost = 100
    -- Variables for the upgrade
    local newSpaces = 10
    local upgradeCost = 100
    

    Check if Players Have Enough Money

    Before selling the player the upgrade, you need to check if they have enough money.

    1. In giveUpgrade, to get the player’s gold and spaces, type:
    local function giveUpgrade(player)
    	print("Someone clicked the button")
    	-- Get's the player's leaderboard to get other IntValues
    	local playerStats = player:FindFirstChild("leaderstats")
    	-- Gets the player's money and spaces to make changes
    	local playerGold = playerStats:FindFirstChild("Gold")
    	local playerSpaces = playerStats:FindFirstChild("Spaces")
    end
    
    Double Check Your Variable Names

    Make sure that the variable name in () is the same variable used in the PlayerSetup script. While this project uses "Gold", your project might be different.


    1. After writing the variables for spaces, create an if statement to if the value of playerGold is more or equal to the upgrade cost.
    local function giveUpgrade(player)
    	local playerStats = player:FindFirstChild("leaderstats")
    	local playerGold = playerStats:FindFirstChild("Gold")
    	local playerSpaces = playerStats:FindFirstChild("Spaces")
    
    	-- Checks if player has enough money to afford the upgrade
    	if playerGold.Value >= upgradeCost then
    		
    	end
    end
    
    1. In the if statement, write a print statement to see if the if statement works when you have enough gold to buy an upgrade.
    -- Checks if player has enough money to afford the upgrade
    if playerGold.Value >= upgradeCost then
    	print("Player can buy item")
    end
    

    Change the Player’s Variables

    Once you’ve checked if a player has enough money, you can give them an upgrade.

    1. Before giveUpgrade, create two variables for how many spaces this upgrade will give and its gold cost.
    local buyButton = script.Parent 
    local clickDetector = buyButton.ClickDetector
    
    -- Variables for the upgrade
    local newSpaces = 10
    local upgradeCost = 100
    
    local function giveUpgrade(player)
    
    1. Under giveUpgrade in the if statement, subtract the upgrade’s cost from the player’s gold.
    if playerGold.Value >= upgradeCost then
    	-- Subtract the item's cost from the player's money
    	playerGold.Value = playerGold.Value - upgradeCost
    end
    
    1. On the next line, change the player’s current spaces to the new number.
    if playerGold.Value >= upgradeCost then
    	playerGold.Value = playerGold.Value - upgradeCost
    	playerSpaces.Value = newSpaces
    end
    
    1. Play your project and check the leaderboard to check if the spaces upgrade worked.
    Upgrades not Working?
    • Make sure anything in the () of FindFirstChild has quotations on both sides, like "leaderstats".
    • Check that each string in FindFirstChild is the exact same as that IntValue’s name in the PlayerSetup script. For example, if your code uses Rubies as money, you should have playerGold = playerStats.FindFirstChild("Rubies")
    • Make sure that giveUpgrade is above clickDetector.MouseClick

    -- Lets players click a button to buy an upgrade that increases MaxSpaces
    local buyButton = script.Parent 
    local clickDetector = buyButton.ClickDetector
    
    -- Variables for the upgrade
    local newSpaces = 10
    local upgradeCost = 100
    
    local function giveUpgrade(player)
    	print("Someone clicked the button")
    	-- Get's the player's leaderboard to get other IntValues
    	local playerStats = player:FindFirstChild("leaderstats")
    	-- Gets the player's money and spaces to make changes
    	local playerGold = playerStats:FindFirstChild("Gold")
    	local playerSpaces = playerStats:FindFirstChild("Spaces")
    	
    	-- Checks if player has enough money to afford the upgrade
    	if playerGold.Value >= upgradeCost then
    		print("Player can buy item")
    		-- Subtract the item's cost from the player's money
    		playerGold.Value = playerGold.Value - upgradeCost
    		playerSpaces.Value = newSpaces
    	end
    end
    
    clickDetector.MouseClick:Connect(giveUpgrade)


    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.