Timers and Custom Events

Timers and Custom Events

Players in Roblox battle royale with timer script icon

Learning Objectives Students will be able to:
  • Use events to signal the start of a match and trigger a timer.
  • Create custom events and use BindableEvents to send signals between server scripts and module scripts.

Using Bindable Events

With players now in the arena, events can be used to signal the start of the match and the code for the timer can begin. Later, an event can also be used to signal the end of the match, and that it’s time to transition players back to the lobby.

These events aren’t prebuilt, so custom event objects called bindable events will need to be created. Bindable events are often used for player-fired actions and are similar to events like Touched or Changed.

Multiple scripts can listen for the same bindable events. This keeps your code organized and makes it easier to add additional code for the start or end of the match later if needed.

Setup Events for Match Start and End

Start by creating bindable event objects for the start and end of the match. Since bindable events don’t interact with the client, they can be stored in Server Storage.

  1. In ServerStorage, create a new folder named Events. In that folder, create two BindableEvents named MatchStart and MatchEnd.

With the events created, they can now be used to code different aspects of the start and end of matches.

Wait for Match End

Right now, when players enter the arena, the intermission keeps restarting rather than beginning the timer. The main game loop needs to be told to stop and wait until the MatchEnd event fires before moving on to the next part of the code.

Events have two built-in functions: Connect() and Wait(). Instead of using Connect() like previously, call Wait() on MatchEnd to pause the game manager script until MatchEnd is fired.

Using Wait() with a custom event is a way to pause code until that script receives that signal. This can be used for a variety of gameplay features:

  • Spawning new traps after a NextLevel event.
  • Creating special effect particles once a team wins a game.
  • Running code when a player says something specific in Chat.

  1. In GameManager, create variables for the Events folder and MatchEnd event.
  1. Have the script wait for the matchEnd event to fire before moving on. In the while true do loop, at the end, type:
  1. Test the game. Confirm that once players are in the arena, the intermission loop doesn’t continue. The script is now waiting for the matchEnd signal to fire.
  • Double check the usage of the dot or colon operators in matchEnd.Event:Wait()
  • Make sure that MatchEnd is a BindableEvent, and not another type, such as a RemoteEvent.

Using a Timer

One of the conditions that will cause the end of the match is a timer running out. To add a timer into the game, use the premade module script in the steps below. It includes functions to start and end a timer, as well as return the amount of time left.

Set Up the Timer Module

  1. In ServerStorage > ModuleScripts, create a new module script named Timer. Replace the code with the Timer code below.

  1. In MatchManager, require:
  • GameSettings - Needed for the match duration variable.
  • Timer - Creates a new timer object to track match time.
  1. Below the variables, create a new timer object by setting a variable named myTimer equal to timer.new(). This object will be used to call functions that start and stop the timer.

The timer module script can be called by other scripts to create more timers as needed, like if you wanted to have traps appear after a specific time. To use the timer in more than one situation, create a new Timer object, like myTrapTimer = timer.new(). Timer objects should only be used for a single purpose, rather than reused.

Start and Stop the Timer

Now that a timer is created, use the included functions start() and stop() during a match. Below is a description of each function and the parameter it accepts.

  • start(time) - Starts the timer, with time in seconds as the parameter.
  • finished:Connect(functionName) - When the timer finishes, runs the function passed as a parameter.
  1. In MatchManager, create a new function named timeUp() to run whenever the timer has finished. Include a test print statement.
  1. Below timeup(), add a function named startTimer() with a print statement. You’ll display the timer in-game later.
  1. To start and stop the timer, in startTimer():
  • Call myTimer.start(). Pass in gameSettings.matchDuration.
  • Call myTimer.finished:Connect(). Pass in timeUp().

Start the Timer with an Event

The timer can be triggered at the start of a match using the Match Start event.

  1. In MatchManager, under the module variables, create variables to store:
  • Events folder
  • MatchStart
  • MatchEnd (used in a future lesson)
  1. Above return MatchManager, connect the matchStart event to startTimer().
  1. To fire the MatchStart event, in prepareGame(), type matchStart:Fire().
  1. Test the game. In the Output Window, confirm that you can see the print statements for the timer’s start and stop functions.
  • Double check usage of capitalization and colons/dot operators. For instancing, when connecting events, always use colons, like in eventName.Event:Connect()
  • Make sure a timer object is created, like in myTimer = timer.new(), before using any timer functions.
  • If the message for the timer stopping doesn’t appear, check in startTimer() that the timer object’s finished event is connected to timeUp()

Finished Project Sample

Project File

Download the finished project.

Finished Scripts

Previous Managing Players Next Creating a GUI