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
Step 3
Create a Cylinder which will represent our coin

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

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
Step 5
Add a Script component to the Coin, name it "Collectible"

Rename the Script to something like "Collectible"

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

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

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

Step 9
Connect the Touched event to our "pickUpCoin" function
Check Your Work
Step 10
Our message should now be output when the coin touches ANYTHING in our game

Open the Ouput using the view tab as shown below:

Step 11
Make it is a player touching the coin
Step 12
Add a Destroy() method to delete the coin after the player touches it

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

Planning
Step 13
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 14
First press the play button so that you can see your player object in the workspace

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

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

Click the (+) and add Sparkles:

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

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

Step 19
Once you have copied the CoinSparkles object, then feel free to press Stop
Step 20
In the Explorer window, paste the CoinSparkles into "ReplicatedStorage"

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

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

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

Step 22
We can use the Clone method to create a clone of the CoinSparkles object
Planning
Step 23
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 24
Store the Coin clone in a reference variable which we can use to set the coin's parent

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

Step 25
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

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

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

Step 32
Now add a TextBox to the ScreenGUI

Rename it to something like PointText

Step 33
Drag the textbox on your screen to where you'd like the points to be displayed
Step 34
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

Step 35
Change the starting Text to 0
Planning
Step 36
When the game is played each player is given a copy of StarterGUI in their PlayerGUI folder

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

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

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

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

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

Account

MVCode

Created By

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