Selling Items

For the next stage of the game loop, players need to sell their items for gold to allow them to purchase more space in their backpacks.

Creating a Sell Platform

Players will sell their items by stepping onto a platform that gives them gold for each item in their bag.

Setting up the Platform

The platform can be any part and will include a script that handles selling.

  1. Create a new part named SellPlatform. Customize it to fit the theme of your experience.

  2. In SellPlatform, create a new script named SellScript and add a comment.

  3. In SellScript, type local sellPart = script.Parent to get the SellPlatform part.


    -- Sells all a player's items and gives them gold
    local sellPart = script.Parent

Handling Touch Events

To use the platform, the script needs a function to check if any players touch it.

  1. Create a function named onTouch() that checks if a player is touching the platform.


    local function onTouch(partTouched)
    local character = partTouched.Parent
    end
  2. To change any of the stats on the leaderboard, the script needs to know what player is controlling the character. In the if statement, use the GetPlayerFromCharacter() function to find a player.


    local Players = game:GetService("Players")
    local player = Players:GetPlayerFromCharacter(character)
  3. On the next line, get that player's leaderstats container.


    local Players = game:GetService("Players")
    local player = Players:GetPlayerFromCharacter(character)
    if player then
    -- Gets the player's leaderboard. Needed to get items and money
    local playerStats = player:FindFirstChild("leaderstats")
    end
  4. On the next line, create variables to get the player's money and items.


    local Players = game:GetService("Players")
    local player = Players:GetPlayerFromCharacter(character)
    if player then
    -- Gets the player's leaderboard. Needed to get items and money
    local playerStats = player:FindFirstChild("leaderstats")
    if playerStats then
    -- Gets the player's items and money
    local playerItems = playerStats:FindFirstChild("Items")
    local playerGold = playerStats:FindFirstChild("Gold")
    end
    end
  5. To check your work, add a print statement that will run if a player touched sellPart.


    local playerItems = playerStats:FindFirstChild("Items")
    local playerGold = playerStats:FindFirstChild("Gold")
    print("A player touched sellPart")
  6. At the bottom of the script, connect the onTouch() to sellPart's Touched event.


    local Players = game:GetService("Players")
    local function onTouch(partTouched)
    local character = partTouched.Parent
    local player = Players:GetPlayerFromCharacter(character)
    if player then
    -- Gets the player's leaderboard. Needed to get items and money
    local playerStats = player:FindFirstChild("leaderstats")
    if playerStats then
    -- Gets the player's items and money
    local playerItems = playerStats:FindFirstChild("Items")
    local playerGold = playerStats:FindFirstChild("Gold")
    print("A player touched sellPart")
    end
    end
    end
    sellPart.Touched:Connect(onTouch)
  7. Play your project and step on sellPart; you should see the message "A Player touched sellPart" in the Output Window.

Selling Items

In this experience, a player will get 100 Gold for each item. After getting money, their items will be set back to 0, letting players explore the world for more items.

Coding a New Sell Function

  1. Under the variables, create a function named sellItems() that gets two parameters named playerItems and playerGold.


    -- Sells all a player's items and gives them gold
    local sellPart = script.Parent
    local function sellItems(playerItems, playerGold)
    end
    local function onTouch(partTouched)
  2. To give players the right amount of gold, take the value of the playerItems and multiply it by the amount of gold they should receive per item. This example gives one hundred gold pieces per item.

    In the sellItems() function, type local totalSell = playerItems.Value * 100


    local function sellItems(playerItems, playerGold)
    -- Gets how many items the player has and multiplies that by item worth.
    local totalSell = playerItems.Value * 100
    end
  3. Type playerGold.Value += totalSell to add the gold for the items to their current gold.


    local function sellItems(playerItems, playerGold)
    local totalSell = playerItems.Value * 100
    -- Add how much the player earns to their money
    playerGold.Value += totalSell
    end
  4. Type playerItems.Value = 0. This sets a player's items back to 0. If a player's items aren't set back to 0, the script will keep giving players gold without stopping.


    local function sellItems(playerItems, playerGold)
    local totalSell = playerItems.Value * 100
    playerGold.Value += totalSell
    playerItems.Value = 0
    end
  5. In the onTouch() function, under the second if statement, call the sellItems() function. Pass in the parameters, playerItems and playerGold so they can be changed.


    local Players = game:GetService("Players")
    local player = Players:GetPlayerFromCharacter(character)
    if player then
    -- Gets the player's leaderboard. Needed to get items and money
    local playerStats = player:FindFirstChild("leaderstats")
    if playerStats then
    -- Gets the player's items and money
    local playerItems = playerStats:FindFirstChild("Items")
    local playerGold = playerStats:FindFirstChild("Gold")
    if playerItems and playerGold then
    sellItems(playerItems, playerGold)
    end
    end
    end
  6. Play your project; check that whenever a player steps on the platform, their gold increases and items are set to 0.

Troubleshooting Tips

At this point, selling items doesn't work as intended, try one of the following below.

  • sellItems() is called in the second if statement that checks the player's items.
  • Any IntValue, like playerItems, uses .Value at the end if you're making a change to it. Value is always capitalized.
  • sellPart.Touched:Connect(onTouch) is typed at the bottom of the script.
  • sellItems(playerItems, playerGold) is typed before the end of the if humanoid then statement.