Buying Upgrades

*เนื้อหานี้จะพร้อมใช้งานในภาษาที่คุณเลือกในเร็วๆ นี้

This brings us to the last stage of the game loop - buying upgrades. By letting players buy upgrades that increase the size of their item bag, they can harvest more items per trip and earn even more gold.

Creating a Shop

Each shop will have a button that players click to purchase a larger item bag. The shop itself will be a part with a SurfaceGUI, an item that allows for text to be written on parts.

Creating the Sign

  1. In the Workspace, create a new model named Shop.

  2. In Shop, create a new block part named BuyButton.

  3. In BuyButton, add a new Surface GUI by clicking the + and scrolling to GUI.

  4. In Surface GUI, add a new TextLabel named BuyText. A small label will appear somewhere on the part.

  5. 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, go into the SurfaceGUI and find the Face property. Change that property until you see the text label.

Changing the Sign 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. Change the Offset for X (left and right) and Y (up and down) to 0.

  2. Change the scale of X and Y to 0.5 to make a square.

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

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

  5. In the BuyText properties, scroll-down and change Text to something descriptive. For instance: "Buy Larger Bag: 100 gold".

  6. Check TextScaled to be on. This automatically scales your text so it fits the box.

Adding 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. ClickDetectors are objects that allow users to interact with something in the environment, such as opening a door.

  1. In the BuyButton, add a ClickDetector.

  2. In BuyButton, add a new script named BuyScript and give it a descriptive comment.

  3. In BuyScript, create variables to store the button part and click detector.


    -- Lets players click a button to buy an upgrade that increases Spaces
    local buyButton = script.Parent
    local clickDetector = buyButton.ClickDetector
  4. 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
  5. After the function, type connect the click detector's MouseClick event to the giveUpgrade() function.


    local function giveUpgrade(player)
    end
    clickDetector.MouseClick:Connect(giveUpgrade)
  6. Add a print statement in giveUpgrade() to test the function.


    local function giveUpgrade(player)
    print("Someone clicked the button.")
    end
  7. Play your project. Click the button and check that you see the text in the Output Window.

Troubleshooting Tips

Issue: You're unable to click the button or a mouse cursor doesn't appear on the button.

  • Make sure that the ClickDetector object is a child of the part you're trying to click on.
  • Check that your character is close enough to the button. Or, make sure that the tool is not equipped.

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.

Adding Upgrade Variables

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

  1. In BuyScript, under local clickDetector, create two variables:

    • newSpace: How many spaces an upgrade adds when purchased.
    • upgradeCost: The cost of a single upgrade

    -- Variables for the upgrade
    local newSpaces = 10
    local upgradeCost = 100

Granting Upgrades

Before selling the player the upgrade, you need to check if they have enough money. If they do, you'll add to their maximum amount of spaces.

  1. In giveUpgrade(), type the following below to get the player's leaderstats so you can access their gold and spaces variables.


    local function giveUpgrade(player)
    print("Someone clicked the button")
    -- Get's the player's leaderboard to get other IntValues
    local playerStats = player:FindFirstChild("leaderstats")
    if playerStats then
    -- Gets the player's money and spaces to make changes
    local playerGold = playerStats:FindFirstChild("Gold")
    local playerSpaces = playerStats:FindFirstChild("Spaces")
    end
    end
  2. 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")
    if playerStats then
    local playerGold = playerStats:FindFirstChild("Gold")
    local playerSpaces = playerStats:FindFirstChild("Spaces")
    -- Checks if player has enough money to afford the upgrade
    if playerGold and playerSpaces and playerGold.Value >= upgradeCost then
    end
    end
    end
  3. In the if statement, subtract the upgrade's cost from the player's gold.


    if playerGold and playerSpaces and playerGold.Value >= upgradeCost then
    -- Subtract the item's cost from the player's money
    playerGold.Value -= upgradeCost
    end
  4. On the next line, add the number of the player's current spaces along with the new spaces granted per upgrade.


    if playerGold and playerSpaces and playerGold.Value >= upgradeCost then
    playerGold.Value -= upgradeCost
    playerSpaces.Value += newSpaces
    end
  5. Play your project and check the leaderboard to check if the spaces upgrade worked.

Troubleshooting Tips

At this point, the upgrades don't work as intended, try one of the following below.

  • 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 FindFirstChild("Rubies").
  • Make sure that giveUpgrade() is above clickDetector.MouseClick.

Complete BuyScript Script

A finished version of the script can be referenced below.


-- 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")
if playerStats then
-- 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 and playerSpaces and playerGold.Value >= upgradeCost then
print("Player can buy item")
-- Subtract the item's cost from the player's money
playerGold.Value -= upgradeCost
playerSpaces.Value += newSpaces
end
end
end
clickDetector.MouseClick:Connect(giveUpgrade)