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

Creating Tools

Creating Tools

Players will need something like an ax or a shovel to gather items with. In Roblox, items that players can equip and use are called tools. This lesson uses a starter tool with all the parts and an animation already made that can be customized later.

Setting up the Tool

To give players a tool at the start of the game, place it into the StarterPack.

Add the Tool to StarterPack

  1. Download the starter tool here if it’s not already on your computer. Remember where you save it to.
  2. In Explorer, under Workspace, right-click on StarterPack.
  1. Select Insert from File.
  1. Find the downloaded starter tool on your computer and open it.
  2. Rename StarterTool to the name you want players to see. For example, Spoon.
  3. Playtest your game. Players should be equipped with the spoon as soon as they start the game.
Tool Controls
  • Press 1 to equip or put away the tool.
  • Left-click to swing the tool.

Setting Up the Tool Script

If the tool hits a harvestable object and the player has enough space in their bag, the player’s item count will go up by 1 on the leaderboard. Harvesting an item will make it disappear for a few seconds and become unharvestable for a few seconds before reappearing. This encourages players to explore to find more items, instead of just clicking the same item.

Create a new Script

  1. Go back to StarterPack, under Spoon, add a new script named ToolScript.
  1. Add a comment at the top.
  2. In ToolScript, type:
-- Gives players items when tool hits cupcakes
local tool = script.Parent
local scoop = tool.Scoop

Check if Item is Harvestable

Whenever the tool touches an object, it’ll check if that object has CanHarvest inside and if the boolean is set to True.

  1. Create a new function named onTouch with a parameter named partTouched.
local tool = script.Parent
local scoop = tool.Scoop

local function onTouch(partTouched)
	
end
  1. In local function onTouch, type local canHarvest = partTouched.Parent:FindFirstChild(“CanHarvest”)
This will search for an object named CanHarvest in whatever touched the tool.
local function onTouch(partTouched)
	local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest")
end
  1. Under local canHarvest, type if canHarvest then and press Enter autocomplete add end
local function onTouch(partTouched)
	local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest")
	if canHarvest then
		
	end
end
  1. Under if canHarvest then, type print("Found an item"). This will help you check if your script is working.
if canHarvest then
     -- Used for testing if code works
	print("Found an item")
end
  1. Under the function’s end, type scoop.Touched:Connect(onTouch). This lets the script check if anything is touching the scoop and if so, will call onTouch.
local function onTouch(partTouched)
	local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest")
	if canHarvest then
		print("Found an item")
	end
end

scoop.Touched:Connect(onTouch)
  1. Play the game and use the tool on a cupcake. Check in the Output Window that you get the message: "Found an Item" when touching that cupcake.
Troubleshooting Tips

If you can’t see the cupcake in the game, make sure it’s unioned part is Anchored.

If you didn’t get the message: Found a Item in the Output Window, try the following:

  • Check that scoop.Touched:Connect(onTouch) is under your function.
  • Check that the object your tool is touching has a BoolValue named CanHarvest under it and that the value is checked.

Get the Leaderboard Stats

Before increasing the player’s items, the tool must find the location of how many items a player has in that player’s leaderboard.

  1. First, get the player using the tool. In the ToolScript, under local scoop = toolScoop, and above the custom function, type:
local scoop = toolScoop

local backpack = tool.Parent
local player = backpack.Parent

local function onTouch(partTouched)
  1. On the next line, find the player’s leaderstats. Type local playerStats = player:FindFirstChild(“leaderstats”)
local backpack = tool.Parent
local player = backpack.Parent
local playerStats = player:FindFirstChild("leaderstats")

local function onTouch(partTouched)
Remember that leaderstats holds the stats for each player. After getting the leaderstats, you can get that player's gold or item count.

  1. Under local playerStats, create variables for the items and spaces stats. Type:
local playerStats = player:FindFirstChild("leaderstats")
-- Gets the player's items and spaces from their leaderboard
local playerItems = playerStats:FindFirstChild("Items")
local playerSpaces = playerStats:FindFirstChild("Spaces")
Check Variable Names in PlayerSetup

The strings inside the () for FindFirstChild need to be the same as the IntValue names in the PlayerSetup script.


Check If the Object is Harvestable

Now that the tool script has the playerItems and playerSpaces variables created, you can start giving players an item point for each cupcake they harvest. Use the function created to check if the object touching the tool can be harvested, and if the player has enough space in their bag to increase the items shown on the leaderboard by one.

  1. To check if the item can be harvested, type if canHarvest.Value == true. Don’t autocomplete the function yet.
local function onTouch(partTouched)
	local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest")
	if canHarvest then
           print("Found an item")
		if canHarvest.Value == true 

	end
end
Getting The Contents of Value Objects

If you want to get a value from an IntValue object or BoolValue object, you need to use .Value at the end.


  1. To check if the player has space for more items, on the same line, add and playerItems.Value < playerSpaces.Value then and press Enter to autocomplete the if statement. If statements within other if statements are called nested if statements.
local function onTouch(partTouched)
	local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest")
	if canHarvest then
		if canHarvest.Value == true and playerItems.Value < playerSpaces.Value then
			
		end
	end
end
Check the Ends Before Moving On

Since you made a nested if statement that has more than one ends, it’s important to check that you don’t have too many ends or too little. Make sure your onTouch function looks exactly the same as above.


  1. Under then, type playerItems.Value = playerItems.Value + 1
if canHarvest then
	if canHarvest.Value == true and playerItems.Value < playerSpaces.Value then
			playerItems.Value = playerItems.Value + 1
			
	end
end
Keep Code Indented For Better Readability

When making nested if statements, it’s especially important to make sure your code has indented spaces, like the above code sample. This helps you see where each if statement starts and ends.


  1. Play your project; use your tool to harvest a cupcake.
Right now, the player's Items stat will instantly fill up after hitting one cupcake. In the next section, you'll make the cupcake reset so players only get one item from each cupcake.

Make the Object Reset

After a player harvests the item, the item should disappear and CanHarvest set to false for a short time before becoming harvestable again.

  1. In onTouch, under playerItems.Value = playerItems.Value + 1, set canHarvest.Value to false.
if canHarvest then
	if canHarvest.Value == true and playerItems.Value < playerSpaces.Value then
		playerItems.Value = playerItems.Value + 1
		canHarvest.Value = false
	end
end
By making the value of canHarvest false as soon as players harvest the item, the script won't give more than one item per tool hit.

  1. After canHarvest.Value = false, type the following:
partTouched.Transparency = 1
partTouched.CanCollide = false
if canHarvest.Value == true and playerItems.Value < playerSpaces.Value then
	playerItems.Value = playerItems.Value + 1
	canHarvest.Value = false
	partTouched.Transparency = 1 
	partTouched.CanCollide = false
end
Changing the transparency to 1 will make the cupcake invisible and making CanCollide false means the player can walk through it. Letting players walk through the object stops having "invisible" cupcakes that block a player.

  1. Type wait(5). This makes the script wait 5 seconds before making the item reappear. You can change 5 seconds to any number that’s fair for your game.
if canHarvest.Value == true and playerItems.Value < playerSpaces.Value then
	playerItems.Value = playerItems.Value + 1
	canHarvest.Value = false
	partTouched.Transparency = 1 
	partTouched.CanCollide = false
	wait(5)
end
  1. After wait(5), type the following to make the cupcake reappear:
	wait(5)
	canHarvest.Value = true
	partTouched.Transparency = 0 
	partTouched.CanCollide = true
end
  1. Play your game and check that:
  • The player only gets 1 item for harvesting a cupcake.
  • The cupcake disappears and then reappears after five seconds.
Harvesting not Working?
  • Check that Transparency and CanCollide are spelled and capitalized exactly.
  • Make sure to use canHarvest.Value and not canHarvest = true.
  • If the cupcake doesn’t reappear make sure that the part for your item is anchored.
  • Check that there is only one part in the Cupcake Model. If you have more than one part, make sure to Union them.
  • Make sure that resetItem is called in the If Statement that checks canHarvest and if the player has enough spaces.



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 Harvestable Items Next Selling Items