Project 580: Minecraft in Roblox - Part Three

4. Let the Pickaxe Break Blocks Based on Toughness

Edit the pickaxe code to uses the 'toughness' value of each block to determine how long it takes to break each block.
Starting point file for this challenge

Your goal

Steps

1. Add a variable 'breakable' after 'myTarget' at the top of the script

Add a new line at the top of your script after you make your 'myTarget' variable, and make a new local variable called 'breakable' and set it equal to 'false'.
local breakable = false

2. Find the line that destroys the block in your 'onClick' function

In your 'onClick' function, find the line of code that uses the 'Destroy()' function on 'myTarget'. 

Make a new line BEFORE that line.

3. Find the toughness of the target block

Use the 'FindFirstChild' function on 'myTarget' to get the toughness value of the target block.
myTarget:FindFirstChild("Toughness").Value

4. Store the toughness in 'breakTime'

Create a new local variable called 'breakTime' and set it equal to the toughness value, using the code from the previous step.
local breakTime = myTarget:FindFirstChild("Toughness").Value

5. Set 'breakable' to true

Set the 'breakable' variable equal to true.
breakable = true

6. Make a for-loop that starts at 1 and repeats 'breakTime' number of times

Create a 'for-loop' that starts the variable 'timer' at 1 and repeats itself the number of times stored inside the 'breakTime' variable.

Don't forget to hit the 'return/enter' key to get an 'end' statement.
for timer=1, breakTime do end

7. Check if 'LeftButtonDown' is false

Use an 'if-then' statement to check if 'LeftButtonDown' is false.

Don't forget to hit the 'return/enter' key to get an 'end' statement.
if LeftButtonDown == false then end

8. If false, set 'breakable' to false

Inside the 'if-then' statement, set the 'breakable' variable equal to false.
breakable = false

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

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

10. Make the Tool the parent of 'anim'

Set the parent of 'anim' equal to 'Tool'. This will cause the animation to play.
anim.Parent = Tool

11. Wait 0.5 seconds, outside the 'if-then-else'

After the end of your 'if-then-else' wait 0.5 seconds.
wait(0.5)

12. Check if 'breakable' is true after the 'for-loop'

After the end of the 'for-loop' use an 'if-then' statement to check if 'breakable' is true.

Don't forget to hit the 'return/enter' key to get an 'end' statement.
if breakable == true then end

13. Move the line that destroys the block into the if-then statement

Move the line that calls the 'Destroy()' function on 'myTarget' to be inside the if-then statement.
myTarget:Destroy()

14. Test it out!

Test out your game to see if your pickaxe can break the blocks when you hold down the mouse button for long enough.