Roblox Heist: Collecting Coins

Planning
Step 1
In this lesson we will be creating collectible coins which award the player points

We will also be learning to add a ScreenGUI to display the number of coins

Step 2
Create a new roblox project and delete the Baseplate
Temp1531951651
Step 3
Create a Cylinder which will represent our coin
Temp1531951725

Also create a Spawn Platform and a little area for the coin to sit upon

Temp1531951861

If you know how to do more advanced modeling, you can try to add a rim around the cylinder like I did, or you can design something besides just a coin.

Step 4
Rename the cylinder to "Coin" in the Workspace tab
Temp1531952143
Step 5
Add a Script component to the Coin, name it "Collectible"
Temp1557008534

Rename the Script to something like "Collectible"

Temp1557008677
Step 6
We'll start the script by creating a variable "player" representing the specific character object containing the script
Temp1557009311
Step 7
Create a function named "pickUpCoin()"
Temp1557009564

Just for now this function will simply print You got a coin! to the screen

Planning
Step 8
In order to call the pickUpCoin() function we need to connect it to a Roblox Event

Roblox Objects let you 'connect' functions to in-game Events

Temp1531958197

Since we want to call the pickUpCoin() function after the coin is touched, we'll connect it to the Touched event

Step 9
The player has many pieces!
Temp1557009849 Temp1557010392
Step 10
Remove the pieces that aren't physical
Temp1557010625 Temp1557010785
Step 11
Connect the Touched event of each part to our "pickUpCoin" function
Temp1557012008
Check Your Work
Step 12
Our message should now be output when we touch ANYTHING in our game
Temp1557012147

Open the Ouput using the view tab as shown below:

Temp1557012361
Step 13
Make sure we are touching a coin
Temp1557012541
Step 14
Add a Destroy() method to delete the coin after the player touches it
Temp1557012684

Note the in Lua we use : when calling object methods (actions)

Planning
Step 15
Next we are going to add a sparkle effect to the player after they pick up a coin

This sparkle is attached to the player's torso so that it follows them as it plays

Step 16
First press the play button so that you can see your player object in the workspace
Temp1531959750

Any changes we make right now are temporary and will disappear when you press "stop".

Step 17
Open the player and navigate down to find the UpperTorso, then add a Sparkles component
Temp1531960356

Click the (+) and add Sparkles:

Temp1531960366
Step 18
Change the sparkle color by selecting it in the workspace explorer and setting "Color" in the data tab
Temp1531960629
Step 19
Once you have the sparkles the way you like them, rename the object to "CoinSparkles"
Temp1531960792
Step 20
Copy the CoinSparkles Object
Temp1531961160

We are going to stop the program in the next step and all of these changes will be deleted

Step 21
Once you have copied the CoinSparkles object, then feel free to press Stop
Temp1531962801
Step 22
In the Explorer window, paste the CoinSparkles into "ReplicatedStorage"
Temp1531962939

This lets us make Clones of the CoinSparkles whenever the player touches a coin in-game

Planning
Step 23
Back in our pickUpCoin() function you can now find CoinSparkles in game.ReplicatedStorage
Temp1531963596

Now we'll be able to use this to create sparkles when the coin is collected

Step 24
We can use the Clone method to create a clone of the CoinSparkles object
Temp1557013626
Planning
Step 25
This won't be enough to see the sparkles though

Objects have to be parented to the Workspace in order to appear in the game world

Information
Step 26
Store the Coin clone in a reference variable which we can use to set the coin's parent
Temp1532025681

This lets us refer to the coin clone as coinSparkles in our Script

Step 27
We can set the Parent of the coin sparkles to be the player's Upper Torso

Now we can set the Coin's parent to the player's Upper Torso

Temp1557013844
Check Your Work
Step 28
Now the coin can be picked up by players and the sparkles will attach to the player's torso
Step 29
Create a Script named "Disappear" on the CoinSparkles object
Temp1532028914
Step 30
This script waits 1 second before destroying the coin
Temp1532029030
Check Your Work
Step 31
Now when we collect a coin, the sparkles will only last a short time
Planning
Step 32
Next we'll make a GUI which displays the number of coins the player has collected
Temp1532031786
Step 33
Add a ScreenGui object to the StarterGui Folder
Temp1532029414

Every player in the game will begin with a GUI copied from this folder

Step 34
Now add a TextBox to the ScreenGUI
Temp1532029526

Rename it to something like PointText

Temp1532029583
Step 35
Drag the textbox on your screen to where you'd like the points to be displayed
Temp1532029678
Step 36
You can change the properties of the text by selecting it and going to the lower-right "Properties" window

I recommend you change TextSize, TextColor3, BackgroundColor3, and BackgroundTransparency

Temp1532029873 Temp1532029900
Step 37
Change the starting Text to 0
Temp1532030010
Planning
Step 38
When the game is played each player is given a copy of StarterGUI in their PlayerGUI folder
Temp1532032000

When the game is played you can see the "PointText" object we need to change is stored here

Temp1532032134

Game.Players[your name].PlayerGUI.ScreenGui.PointText

Step 39
So we'll go ahead an insert that long line of code right into our Coin's "Collectable" script
Temp1557014389

And then we'll increase PointText's "Text" value by 1

Check Your Work
Step 40
You should now be able to collect the coin and see your points increase by 1
Check Your Work
Step 41
Once you have it working you can copy and paste to add as many coins as you'd like
Planning
Step 42
In the next lesson we will be making an exploding trap
Roblox Heist: Collecting Coins Info

Account

MVCode Clubs

Created By

Newprofile jamie

Course:

Roblox Heist Game

Access Level

public

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach