Project 580: Minecraft in Roblox - Part Three

8. Allow the Player to 'Pick Up' the Blocks They Break

Add a new function 'pickUp' to your Pickaxe code which determines which type of block the player is picking up and adds one to the amount for that block, 
Starting point file for this challenge

Your goal

Steps

1. In your Pickaxe code, create the 'pickUp' function BEFORE the 'onClick' function

Make space above your 'onClick' function in your pickaxe code. In that space, write the new function 'pickUp' which has one parameter called 'block'. 

Don't forget to hit the 'return/enter' key to get an 'end' statement.
function pickUp(block) end

2. Store the player in 'myPlayer'

Make a new local variable called 'myPlayer' and set it equal to the local player inside the games list of players.
local myPlayer = game.Players.LocalPlayer

3. Find the player's backpack

Make a new local variable called 'backpack' and set it equal to 'myPlayer.Backpack'. You will use this to check if the player is holding a certain block or not.
local backpack = Tool.Parent.Backpack

4. Make a local variable 'toolBlock'

Create a new local variable called 'toolBlock'. Do NOT set it equal to anything.
local toolBlock

5. Check if the material of 'block' is Slate

Use an 'if-then' statement to check if 'block.Material' is equal to 'Enum.Material.Slate'. Hit return to get an end statement.

If you made your block tool a different material, use that instead of slate.
if block.Material == Enum.Material.Slate then end

6. Find the 'Stone' tool in backpack and store it in 'toolBlock'

Use the 'FindFirstChild()' function on 'backpack' to find the Stone tool block in the player's inventory. Store the result in the 'toolBlock' variable.

If you named your block tool something different, use that instead of 'Stone'.
toolblock = backpack:FindFirstChild("Stone")

7. Check if 'toolBlock' is empty

Use an 'if-then' statement to check there is anything stored in 'toolBlock'. Hit return to get an end statement.

If there is nothing in 'toolBlock' that means the player does not have a tool block for that material in their inventory.
if not toolblock then end

8. Return if 'toolblock' is empty

Use the word 'return' to exit the function if the player does not have a block tool for that material.
return

9. Add an 'else' to your 'if-then' statement

Type the word 'else' on the next line and hit 'return/enter'. 
else end

10. Add 1 to the value of 'Amount' in 'toolblock'

Set 'toolblock.Amount.Value' equal to 'toolblock.Amount.Value + 1'.
toolblock.Amount.Value = toolblock.Amount.Value + 1

11. Call the 'pickUp' function before destroying the block

Make a new line BEFORE the line in the 'onClick' function that destroys 'myTarget'. Call the 'pickUp' function with 'myTarget' as a parameter
pickUp(myTarget)