Roblox Heist: Police Chase

Step 1
First create a Timer in the same way you did your Points text box
Temp1532651008 Temp1532650969

Make it big and noticeable in the center of your screen

Step 2
Set the timer's "Visible" property to false
Temp1532651363

Untick the checkbox so the timer starts out invisible.

Step 3
Add a new Local Script to your player's Starter Scripts. This script with play the timer sequence
Temp1532651144

Because my timer is police themed, I named it PoliceChase.

You can name yours whatever you'd like.

Step 4
This script will store a "global function" named 'StartTimer'

_G. signifies that this is a global function

Temp1532652279

A global function can be called from any other script.

Step 5
For now this function will simply print a message
Temp1532653344

We're next going to learn how to call this global function from another script...

Planning
Step 6
In the next few steps we will be adding a script to the Rare Jewel which will call the global StartTimer() function
Temp1532653233

Start by adding a script named PickUpJewel to the Rare Jewel.

Step 7
First we'll have the script identify the jewel object
Temp1532653651

The script is a child of the jewel and thus script.parent refers to the jewel itself.

Step 8
Create a function named "onEquipped" and connect it to the "Equipped" Event
Temp1532653750

The Equipped Event is built-in to Roblox
onEquipped is a function we just now created

Step 9
Using _G call the global StartTimer() function
Temp1532654457

When the jewel is equipped, we call StartTimer().

Check Your Work
Step 10
Calling the global function in the pick-up script should activate it in the timer script

The _G stores these functions

Temp1532654385

You in fact see that the function is called when you pick up the gem

Temp1532656074
Planning
Step 11
We have one problem to fix though, the StartTimer() function is called *every time* the player takes out the jewel

We only want it to start the timer the first time it is equipped.

Step 12
We will add a boolean to check if the jewel has been pickedUp already
Temp1532656492

When the game starts, pickedUp is initialized as false.

Step 13
Here we say to only start the timer if the jewel hasn't been picked up yet
Temp1532656685

Note that we set pickedUp = true so that StartTimer won't be called again.

Check Your Work
Step 14
Verify that StartTimer is only called once no matter how many times you equip the jewel
Planning
Step 15
Now that that's taken care of, we're going to build this script together

The three phases of StartTimer():

  • Display some kind of "Escape before time runs out!" message
  • Create a while loop to count down
  • Kill the player if the timer hits 0
Step 16
First, our script need to reference the player
Temp1532658421

We can see on the right, that the player is the script's -> parent's -> parent.

Step 17
We also need to reference the timer GUI object
Temp1532659548

We initialize the timer once the StartTimer function is called because timer GUI objects are destroyed whenever the player dies, and that'd be a problem if we only initialized it to be the first timer at the start of the game.

Step 18
Now we can use the timer reference variable to tell our timer GUI to appear and display text
Temp1532659787
Check Your Work
Step 19
You should see your message display when you grab the jewel
Temp1532659838
Step 20
Next we will have the script wait 2 seconds before starting a countdown timer

timeRemaining starts at 100 and while it is greater than 0, we subtract 1 from timeRemaining:

Temp1532660071
Step 21
Then we can update timer's Text to display the timeRemaining
Temp1532660364
Check Your Work
Step 22
If we hit Run right now, the timer will instantly count down to 0

While loops run every loop in a single frame unless told to wait()

Step 23
In Lua, we can use wait() to make the while loop wait a single frame before repeating
Temp1532660661

You could also say wait(1) to count in seconds rather than frames

Step 24
After the while loop ends we'll have the timer announce the player has run out of time and set their health to 0
Temp1532661119

Brackets [ ] are an alternate way of using .

This code is the same as workspace.MVCCJamie since player.name == "MVCCJamie"

Check Your Work
Step 25
Test that the player now dies if the timer runs out
Step 26
Create a second global function named StopTimer
Temp1532661674
Step 27
This function will make the timer invisible when it is called
Temp1532661757

Of course, the timer will still be ticking in the background...

Step 28
Add an if statement so that when the timer runs out, it only kills the player if it is visible
Temp1532661947

So when StopTimer() is called, the timer becomes invisible, which means it won't kill the player when it runs out.

Challenge
Step 29
Create and "End" object which calls "StopTimer()" if a player touches it

I made an invisible block with some Sparkles to signify my "end" object

Temp1532662337

Hints:

  • The script needs a reference to the Part
  • The script should Connect a function to the Part's Touched method
  • Use _G. to call a global function
  • Set your timeRemaining to start at a higher value
Check Your Work
Step 30
Your timer should now stop running when the player reaches the "End" of the course
Planning
Step 31
Extras

You've now reached the end of the course, and it's time for you to make this game your own.

Here are some ideas to get you started:

To my game I added:

  • a flashing red and blue police light to the player
  • made the timer count in minutes and seconds
  • made the timer flash red and blue
Step 32
Here i how I created the flashing Red and Blue light
Temp1532663509

I created a point light inside of ReplicatedStorage and attached a script named flash

Step 33
The flash script uses a back-and-forth (sine) tween to change the light color between red and blue
Temp1532663841

TweenInfo takes inputs in this order:

  • time
  • style (sine for back-and-forth)
  • direction
  • # of times to repeat (-1 for infinite)
  • reverse direction? (true/false)
Information
Step 34
I used the same technique to change the timer background color
Step 35
That's it! What else can you think to add?

Some ideas:

  • enemies
  • different types of treasure
  • lava/water
  • bigger level
  • buttons that control other things
Roblox Heist: Police Chase Info

Account

MVCode Clubs

Created By

Newprofile jamie

Course:

Roblox Heist Game

Access Level

premium

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