Timers and Custom Events
Timers and Custom Events
Students will be able to:
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
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.
- 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:
Wait(). Instead of using
Connect() like previously, call
Wait() on MatchEnd to pause the game manager script until MatchEnd is fired.
Using Event:Wait() in Other Ways »
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
- Creating special effect particles once a team wins a game.
- Running code when a player says something specific in Chat.
- In GameManager, create variables for the Events folder and MatchEnd event.
- Have the script wait for the
matchEndevent to fire before moving on. In the
while true doloop, at the end, type:
- Test the game. Confirm that once players are in the arena, the intermission loop doesn’t continue. The script is now waiting for the
matchEndsignal to fire.
Troubleshooting Tips »
- Double check the usage of the dot or colon operators in
- Make sure that
MatchEndis 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
- In ServerStorage > ModuleScripts, create a new module script named Timer. Replace the code with the
- In MatchManager, require:
- GameSettings - Needed for the match duration variable.
- Timer - Creates a new timer object to track match time.
- Below the variables, create a new timer object by setting a variable named
timer.new(). This object will be used to call functions that start and stop the timer.
Using the Timer in Other Gameplay Scenarios »
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
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.
- In MatchManager, create a new function named
timeUp()to run whenever the timer has finished. Include a test print statement.
timeup(), add a function named
startTimer()with a print statement. You’ll display the timer in-game later.
- To start and stop the timer, in
myTimer.start(). Pass in
myTimer.finished:Connect(). Pass in
Start the Timer with an Event
The timer can be triggered at the start of a match using the Match Start event.
- In MatchManager, under the module variables, create variables to store:
- Events folder
- MatchEnd (used in a future lesson)
return MatchManager, connect the
- To fire the MatchStart event, in
- Test the game. In the Output Window, confirm that you can see the print statements for the timer’s start and stop functions.
Troubleshooting Tips »
- Double check usage of capitalization and colons/dot operators. For instancing, when connecting events, always use colons, like in
- 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
Finished Project Sample
Download the finished project.
Previous Managing Players Next Creating a GUI