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
- In the Workspace, create a new model named Shop.
- 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.
- In BuyButton, add a new Surface GUI by clicking the + and scrolling to GUI.
- In Surface GUI, add a new TextLabel named BuyText. A small label will appear somewhere on the part.
Can't Find the Label? »
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:
- In the Properties for Surface GUI, scroll-down to Data.
- 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.
- In the BuyText properties, click on the arrow next to Size.
- Change offset for X (left and right) and Y (up and down) to 0.
- Change the scale of X and Y to 0.5 to make a square.
- 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.
- 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.
- 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).
- 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.
- In the BuyButton, add a ClickDetector.
- In BuyButton, add a new script named BuyScript and give it a descriptive comment.
- In BuyScript, type:
-- Lets players click a button to buy an upgrade that increases Spaces local buyButton = script.Parent local clickDetector = buyButton.ClickDetector
- Create a new function named
giveUpgradethat 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
- After the function, type
clickDetector.MouseClick:Connect(giveUpgrade). Whenever players click on the part, this line will call
local function giveUpgrade(player) end clickDetector.MouseClick:Connect(giveUpgrade)
- Add a print statement in
giveUpgrade, and then test the function.
local function giveUpgrade(player) print("Someone clicked the button") end
- 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.
- 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.
- 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.
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.
- 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
- 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.
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.
- 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
- 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.
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)
giveUpgradein 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
- 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
- Play your project and check the leaderboard to check if the spaces upgrade worked.
Upgrades not Working?
- Make sure anything in the
FindFirstChildhas quotations on both sides, like
- Check that each string in
FindFirstChildis 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
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.
Previous Selling Items Next Getting Your Game Ready