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
- Download the starter tool here if it’s not already on your computer. Remember where you save it to.
- In Explorer, under Workspace, right-click on StarterPack.

- Select Insert from File.

- Find the downloaded starter tool on your computer and open it.
- Rename StarterTool to the name you want players to see. For example, Scoop.
- Playtest your game. Players should be equipped with the scoop 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
- Go back to StarterPack, under Scoop, add a new script named ToolScript.

- Add a comment at the top.
- 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.
- Create a new function named
onTouch
with a parameter namedpartTouched
.
local tool = script.Parent local scoop = tool.Scoop local function onTouch(partTouched) end
- In
local function onTouch
, typelocal canHarvest = partTouched.Parent:FindFirstChild(“CanHarvest”)
local function onTouch(partTouched) local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest") end
- Under
local canHarvest
, typeif canHarvest then
and press Enter autocomplete addend
local function onTouch(partTouched) local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest") if canHarvest then end end
- Under
if canHarvest then
, typeprint("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
- Under the function’s
end
, typescoop.Touched:Connect(onTouch)
. This lets the script check if anything is touching the scoop and if so, will callonTouch
.
local function onTouch(partTouched) local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest") if canHarvest then print("Found an item") end end scoop.Touched:Connect(onTouch)
- 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.
- Make sure that the harvestable object is a Model. The code assumes there is a CanHarvest object attached to a model.
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.
- 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)
- 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)
- 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.
- 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.
- 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.
- Under
then
, typeplayerItems.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.
- Play your project; use your tool to harvest a 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.
- In
onTouch
, underplayerItems.Value = playerItems.Value + 1
, setcanHarvest.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
canHarvest
false as soon as players harvest the item, the script won't give more than one item per tool hit.
- 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
CanCollide
false means the player can walk through it. Letting players walk through the object stops having "invisible" cupcakes that block a player.
- 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
- After
wait(5)
, type the following to make the cupcake reappear:
wait(5) canHarvest.Value = true partTouched.Transparency = 0 partTouched.CanCollide = true end
- 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 notcanHarvest = 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.
Finished Tool Script »
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 Session Lesson Plan Next Selling Items