Step 4 – ToolScript

Begin Coding

Copy the code in the image above into your ToolScript. You will start on line 13.

  1. To check if the item can be harvested, type if canHarvest.Value == true. Don’t autocomplete the function yet.
  2. 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.
  3. Under then, type playerItems.Value = playerItems.Value + 1

Check if Item is Harvestable Cont.:

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.

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

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 you want to get a value from an IntValue object or BoolValue object, you need to use .Value at the end.

Playtesting

Playtest your game and press the number 1 to pull out your spoon. Now try to collect your item with your spoon by clicking on it.