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

Selling Items

Selling Items

What should players do when they have a bag full of cupcakes? Sell them of course! For the next stage of the game loop, players need to sell their sweet cupcakes for even sweeter gold that allows them to purchase a larger cupcake box.

Creating a Sell Platform

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

Create a Part and Script

  1. Create a new part named SellPlatform. Customize it to fit the theme of your game.
  1. In SellPlatform, create a new script named SellScript and add a comment.
  1. 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

Create a Touch Event

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

  1. Create function named onTouch that checks if a player is touching the platform. Try not to peak at the code box!
local function onTouch (partTouched)
	local character = partTouched.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	
	if humanoid then
		
	end
end

  1. To change any of the stats on the leaderboard, the script needs to know what player is controlling the humanoid. In the if humanoid statement, type local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
if humanoid then
	local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
end
  1. On the next line, type local playerStats = player:FindFirstChild(“leaderstats”)
    to get the player’s leaderboard.
if humanoid then
	local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	-- Gets the player's leaderboard. Needed to get items and money
	local playerStats = player:FindFirstChild("leaderstats")
end
  1. On the next line, create variables to get the player’s money and items.
if humanoid then
	local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	-- Gets the player's leaderboard. Needed to get items and money
	local playerStats = player:FindFirstChild("leaderstats")
	-- Gets the player's items and money
	local playerItems = playerStats:FindFirstChild("Items")
	local playerGold = playerStats:FindFirstChild("Gold")
end
Check Variable Names in PlayerSetup

Make sure the names of everything in FindFirstChild is exactly like the names written in the PlayerSetup script. For example, if your money is "Rubies" in PlayerSetup, local playerGold should look for "Rubies" instead of "Gold".


  1. 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")
  1. Under onTouch, type sellPart.Touched:Connect(onTouch) to connect the function to sellPart’s Touched event.
local function onTouch (partTouched)
	local character = partTouched.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		-- Gets the player's leaderboard. Needed to get items and money
		local playerStats = player:FindFirstChild("leaderstats")
		-- Gets the player's items and money
		local playerItems = playerStats:FindFirstChild("Items")
		local playerGold = playerStats:FindFirstChild("Gold")
        print("A player touched sellPart")
	end
end

sellPart.Touched:Connect(onTouch)
  1. Play your project and step on sellPart; you should see the message "A Player touched sellPart" in the Output Window.

Selling Items

In this game, 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.

Create and Set Up the sellItems() Function

  1. Under local sellPart = script.Parent, 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)
Separate Multiple Parameters with Commas

Functions can get more than one parameter, but each one must be separated by a comma, like in parameter1, parameter2.


  1. 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
  1. Type playerGold.Value = 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 = playerGold.Value + totalSell
end
  1. 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 = playerGold.Value + totalSell
	playerItems.Value = 0
end
Now that the sellItems function is created, it can be called in onTouch to give whatever player touches sellPart.

  1. In the onTouch function, under the second if statement, call the sellItems function. Type sellItems(playerItems, playerGold). This will gives the player’s items and money to the sellItems.
if humanoid then
	local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	-- Gets the player's leaderboard. Needed to get items and money
	local playerStats = player:FindFirstChild("leaderstats")
	-- Gets the player's items and money
	local playerItems = playerStats:FindFirstChild("Items")
	local playerGold = playerStats:FindFirstChild("Gold")
	sellItems(playerItems, playerGold)
end
  1. Play your project; check that whenever a player steps on the platform, their gold increases and items are set to 0.
Troubleshooting the Sell Platform

Check that:

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



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 Creating Tools Next Upgrades