With the leaderboard created, players need something to collect. For that, you'll need to create a 3D item for players to find in the world. Below is a video of the process of players harvesting items.
Build an Item
Items in the experience will be 3D models that players use a tool to harvest. Each item, once harvested, will disappear and then reappear after a limited amount of time.
For the item, reference back to your game design document. This series will use crystals as the example.
Create an object, either using parts or objects found by trusted users in the Marketplace.
If desired, the crystal part can be downloaded with this link. To add it, right click on the Workspace and pick Insert From File.
If using your own part, group all the parts into a Model. One method of doing this is to select all items, right click on a part, and select Group. This creates a model that organizes your parts.
Make sure that the parts are all anchored.
So items cannot be harvested while they disappear, you'll use a BoolValue to track its status. In the Item model, create a BoolValue named CanHarvest.
In Properties for CanHarvest, check the Value box. Checking the value box makes the boolean true, meaning players can harvest that item.
Creating a Tool
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.

Adding the Tool
A starter tool will be provided for this series. For players to use it, it needs to be placed inside the StarterPack.
In Explorer, under Workspace, right-click on StarterPack. Then, select Insert from File.
Select the downloaded file: starterTool.rbxm.
Playtest your project. Players should be equipped with the tool as soon as they start. In-game, press 1 to equip or put away the tool, or left-click to swing it.
Coding the Tool
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.
Setting up the Script
The tool will include a script that handles what happens whenever it touches objects it can harvest.
In StarterPack, under StarterTool, add a new script named ToolScript.
In the script, write a descriptive comment at top, and then create variables to store the tool part and the tool itself.
-- Gives players item when they touch a harvestable partlocal toolPart = script.Parentlocal tool = toolPart.Parent
Checking for Items
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 named partTouched.
local toolPart = script.Parentlocal tool = toolPart.Parentlocal function onTouch(partTouched)endIn that function, create a local variable named canHarvest. Then, use the FindFirstChild() function to see if there is CanHarvest boolean in the parent of that part.
local function onTouch(partTouched)local canHarvest = partTouched:FindFirstChild("CanHarvest")endNow the script needs to check if there was actually anything found and if so, run code. To do this, create an if statement where the condition is canHarvest. If anything exists in canHarvest, this statement will be true.
local function onTouch(partTouched)local canHarvest = partTouched:FindFirstChild("CanHarvest")if canHarvest thenendendIn the if statement, add a print statement to see if the script is working. You'll code the logic for harvesting items once you're sure it works.
if canHarvest then-- Used for testing if code worksprint("Found an item")endUnder the function's end, type tool.Touched:Connect(onTouch). This lets the script check if anything is touching the tool and if so, will call onTouch().
local function onTouch(partTouched)local canHarvest = partTouched:FindFirstChild("CanHarvest")if canHarvest thenprint("Found an item")endendtool.Touched:Connect(onTouch)Play the project and use the tool on a harvestable item (left-click to swing). Make sure you see the message "Found an item" in the Output Window.
Troubleshooting Tips
If you don't see the message, try the following tips.
- If you're using custom parts and meshes, it's possible to get an error. The script only works if the CanHarvest object is a child of the part the tool is touching.
- Make sure that the tool is in the StarterPack, not in the Workspace.
- Check that the part is anchored.
Getting Player 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. Once the tool has access to the leaderboard, it can change that player's item count.
First, get the player using the tool. In the ToolScript, under local item = toolitem, and above the custom function, type:
local item = toolitemlocal backpack = tool.Parentlocal player = backpack.Parentlocal function onTouch(partTouched)On the next line, find the player's leaderstats using the FindFirstChild() function.
local backpack = tool.Parentlocal player = backpack.Parentlocal playerStats = player:FindFirstChild("leaderstats")local function onTouch(partTouched)Under local playerStats, create variables to store the items and spaces stats.
local playerStats = player:FindFirstChild("leaderstats")local playerItems = playerStats:FindFirstChild("Items")local playerSpaces = playerStats:FindFirstChild("Spaces")
Checking for a Harvestable Object
Now that the tool script has the playerItems and playerSpaces variables created, you can start giving players an item. 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.
The script will need an if statement with two conditions to be met. Start by creating an if statement, then add in the following conditions, connected with the and keyword. .
- canHarvest.Value == true
- playerItems.Value < playerSpaces.Value
local function onTouch(partTouched)local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest")if canHarvest thenif canHarvest.Value == true and playerItems.Value < playerSpaces.Value thenendendendIn the if statement itself, add to the player's items by typing playerItems.Value = playerItems.Value + 1.
if canHarvest thenif canHarvest.Value == true and playerItems.Value < playerSpaces.Value thenplayerItems.Value = playerItems.Value + 1endendPlay your project; use your tool to harvest an item and check that the item count went up.
Resetting the Item
When the item is harvested, it will reset in two ways:
- The item will disappear and not be interactable
- CanHarvest set to false
The item will then go back to normal after a short time. This way, players only get one item for each harvest, and need to look around for more while the original resets.
Under where items are added, set canHarvest to false. 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.
if canHarvest thenif canHarvest.Value == true and playerItems.Value < playerSpaces.Value thenplayerItems.Value = playerItems.Value + 1canHarvest.Value = falseendendAfter setting the value to false, set the part's Transparency to 1 (invisible), and CanCollide to false, meaning players can't touch it.
if canHarvest.Value == true and playerItems.Value < playerSpaces.Value thenplayerItems.Value = playerItems.Value + 1canHarvest.Value = falsepartTouched.Transparency = 1partTouched.CanCollide = falseendType wait(5) to give time for the item to reset. 5 is a suggested number, and maybe differ for how long you'd like for your experience.
if canHarvest.Value == true and playerItems.Value < playerSpaces.Value thenplayerItems.Value = playerItems.Value + 1canHarvest.Value = falsepartTouched.Transparency = 1partTouched.CanCollide = falsewait(5)endAfter the wait, do the opposite of previous code, by setting the CanHarvest to true, and resetting the Transparency and CanCollide to their original values.
wait(5)canHarvest.Value = truepartTouched.Transparency = 0partTouched.CanCollide = trueendPlay the project and check:
- The player only gets 1 item for harvesting a item.
- The item disappears and then reappears after five seconds.
Troubleshooting Tips
At this point, if one of the checks didn't pass try one of the following below.
- Check that Transparency and CanCollide are spelled and capitalized exactly.
- Make sure to use canHarvest.Value and not canHarvest = true.
Complete ToolScript
A finished version of the script can be referenced below.
local toolPart = script.Parent
local tool = toolPart.Parent
local backpack = tool.Parent
local player = backpack.Parent
local playerStats = player:FindFirstChild("leaderstats")
local playerItems = playerStats:FindFirstChild("Items")
local playerSpaces = playerStats:FindFirstChild("Spaces")
local function onTouch(partTouched)
local canHarvest = partTouched:FindFirstChild("CanHarvest")
if canHarvest then
if canHarvest.Value == true and playerItems.Value < playerSpaces.Value then
playerItems.Value = playerItems.Value + 1
canHarvest.Value = false
-- Reset partTouched, the harvested item
partTouched.Transparency = 1
partTouched.CanCollide = false
wait(5)
-- Make the harvested item reappear and usable again
canHarvest.Value = true
partTouched.Transparency = 0
partTouched.CanCollide = true
end
end
end
toolPart.Touched:Connect(onTouch)