UevKF8zjRV_WEtpJsXhr
We use cookies on this site to enhance your user experience

Coding the Game Loop

Coding the Game Loop

Person in obstacle course for Roblox battle royale lesson

Learning Objectives Students will be able to:
  • Practice modular programming by creating scripts that will separately handle variables, matches and manage a game loop.
  • Simulate a multiplayer game using local servers to test code in realistic online situations.
  • Combine and nest together a while and repeat loop running the game loop.

Setting up the Scripts

Script Overview

The battle royale will use a combination of module scripts and normal scripts. The module scripts GameSettings and MatchManager will hold commonly used functions and variables. A normal script named GameManager will manage the main game loop. Below are specific tasks for each script:

GameManager (Script) MatchManager (Module) GameSettings (Module)
Runs functions from the Match Manager using variables from the Game Settings Runs functions like sending players into an arena or keeping track of time in a match. Stores commonly used variables

Store Variables in GameSettings

Create a module script named GameSettings to store variables used by other scripts, like match and intermission duration. These variables will be used by the GameManager script later.

  1. In ServerStorage, create a folder named ModuleScripts. In that folder, create a new module script named GameSettings.
  1. Open GameSettings and rename the module table to match the name of the script.
  1. In the module table, add variables for the following uses. Take your best guess for each value, you can always change it later as you test.
Example: GameSettings.variableName = 5
  • Intermission Duration - Seconds players wait before a match.
  • Match Duration - Length of a match in seconds.
  • Minimum Players - Smallest number of players needed to start.
  • Transition Time - Time before and after a match in seconds. Makes transitioning between parts of the game loop less sudden.

Create the Match Manager Script

The second script connected to the GameManager is the MatchManager. This script manages tasks like starting the timer or resetting players once the match ends.

Within MatchManager is a function named preparePlayers() that transitions players from the intermission to the match.

  1. In ServerStorage > ModuleScripts > add a module script named MatchManager. Rename the module table.
  1. Add a new module function to MatchManager named preparePlayers(). Include a print statement to test the script later.

Coding the Game Loop

The main game loop will be coded in the GameManager script using the variables just created. Remember, there are 3 phases in the game loop; intermission, competition, and cleanup & reset.

Intermission
Competition
Cleanup and Reset

Create a GameManager

This script is a normal server script, so put it in ServerScriptService, rather than the module scripts folder. The actual game loop will be in a while true do loop.

  1. In ServerScriptService, create a new script named GameManager.
  1. Add variables for the following services:
  • ServerStorage - Gets the location of module scripts.
  • Players - Checks player count during intermissions.
  1. To use the previously created modules:
  • Set a variable named moduleScripts equal to the location of the ModuleScripts folder.
  • Add variables named matchManager and gameSettings. Set each variable to require their respective script.
  1. After the variables, add a while true do loop. All phases of the game loop will go inside to repeat indefinitely.

Code the Intermission

While the game loop runs indefinitely, the intermission should pause the loop and only continue when there are enough players for a match. To code this pause, include a nested repeat loop for the intermission in the while loop. That nested loop will repeat until there are enough players, pausing the main loop. Once there are enough players, it’ll exit and transition players into a match.

With a repeat loop, the code in the loop will run at least once. Unlike a while loop, it doesn’t check it’s condition until the loop ends. This ensures players always go to the lobby before a match.

  1. In the while true do loop, type repeat and press Enter to autocomplete with until.
  1. Check if the current number of players (Players.NumPlayers) is greater or equal to the minimumPlayers variable created earlier in the GameSettings module.
  1. In the repeat loop, add a print statement saying the intermission is starting. Use wait() to pause for the intermission using intermissionDuration from GameSettings.
  1. Playtest and check that the print statement is shown at least twice. Seeing the message twice proves the repeat loop didn’t find enough players and ran again. You’ll have to wait the length of intermission before seeing the message a second time.
Spawn Locations are Picked at Random

Players will spawn at a random spawn, which may or not be in the lobby. In a later lesson, you’ll set all players to spawn in the lobby when joining a game.


Issue: Error saying "Game script timeout".

  • wait() should be inside the repeat loop. Without the wait, the script will run too many times in a second, overloading Roblox Studio and causing an error.
  • In the Game Settings module, the variable intermissionDuration should be greater than 1. If lower, the script can repeat too often, causing slow down issues.

End the Intermission

Once there are enough players, have them wait a short transition time. Then, send them into the match by calling GameManager’s preparePlayers(). Remember, that function just prints a line, but you’ll add more code later.

  1. After the repeat loop, add a print statement to test your code, followed by a wait() using GameSetting’s transitionTime variable.
Keep Code in the While Loop

If code is outside of the while true do loop, parts of your game loop might not repeat and players will just be stuck in the intermission phase.


  1. After the wait(), call the prepareGame() from the MatchManager module. When the code runs, this will just print text to the output window. Wait until the next section to test this code.

Code Below the While Loop Won't Run

If you add onto the script after this project, keep in mind code below the while true do loop won’t run. Keep relevant code inside the while loop or call module functions from the main loop.


Testing Multiplayer Games

Right now, to have the code run preparePlayers(), it needs to exit the repeat loop. But, to do that, there needs to be more than one player. This means if you use the playtest button, the function will never run because you’re the only player in the game (unless your minimum players is one).

To test code requiring more than one player, create a local server. While published games are normally on Roblox servers, a local server simulates a multiplayer game on your computer with simulated players.

Start Local Server

  1. To start a local server, in the Test tab > Clients and Servers section > set the player dropdown to the number of players in GameSetting’s variable minimumPlayers. This lesson uses 2 players. Click Start to begin the server.
  1. Wait a few seconds for the server to set up. Multiple windows will open in addition to your original Studio window. You may need to allow access to Roblox Studio from firewalls or other online security software.

Firewalls
If you have any issues with the server starting, double check the article Firewall and Router Issues.

Error Messages or Servers not Running
You may get an error message saying “Failed to connect to the game”. To address this:

  • Make sure you’re not running any extra applications in the background (such as internet browsers, etc).
  • Set the number of players to a small amount, like 2 or 3.
  • If the issue doesn’t resolve, try restarting Studio or restarting your computer.

Testing in the Local Server

One will be the Server (green border), which runs the game. The rest will be Clients (blue borders), which simulate players you can control.

Server Window surrounded by a green border
Client window surrounded by a blue border

To start a test, do the following:

  1. Find the Server window with the green border. Check for the print statement called from the MatchManager script.
Differences between Client and Server Output

Different types of scripts will print to different windows. For instance, scripts will print in the Server window, local scripts will print in the client. For more information, check out this article on Game Testing.


  • Check that functions like prepareGame() are in scope of the while true do loop.
  • If the print from MatchManager didn’t work, check some common troubleshooting with module scripts, like making sure that the MatchManager script is required in GameManager or that prepareGame() is added to that module’s table.

  1. Once you’re done testing, in any window, close the server by Cleanup button. This closes all Server and Client windows and takes you back to your normal Studio window.

Finished Project Sample

Project File

Download the finished project here.

Finished Scripts





Previous Project and Map Setup Next Managing Players