Managing Players

Managing Players

Picture of player in Roblox battle royale

Learning Objectives Students will be able to:
  • Manage players in a multiplayer game by creating a PlayerManager script and storing a list of active players in a table.
  • Set up events to load players into the lobby when starting a game.
  • Use for loops and pairs to iterate through a table of players and run a function to send each player to a game with a tool.

The Player Manager Script

During a match, players can come and go. Because of this, code is needed for tasks like sending players into a match and keeping track of active players. To manage these tasks, create a module script named PlayerManager.

This script will start a function to send players into the arena with a weapon and be expanded later in the series.

Set Up the Manager

  1. In ServerStorage > ModuleScripts, add a new module script named PlayerManager.
  • Rename the module table to match the script name.
  • Add comments to create sections for your variables, local functions, and module functions. This will help keep you organized.
  1. Add local variables for the following:
  • Services:
    • Players - Know what players have joined or left the game.
    • ServerStorage - Storage for player weapons.
  • Map and Player Variables:
    • Lobby Spawn, Arena Folder, and Arena Spawn Folder - Used to teleport players to different areas.
    • An Array of Active Players - Keeps track of players currently in a game.
  1. Create a module function named sendPlayersToMatch() with a test print() inside.

Spawn Players in the Lobby

Instead of randomly spawning when joining, spawn players in the lobby by changing the player’s RespawnLocation property.

  1. Create a new local function named onPlayerJoin() with a parameter of player. In that function, set the player RespawnLocation property to the lobby spawn variable made earlier.
  1. Add an events section beneath your module function. Then, connect onPlayerJoin() to Player Service’s PlayerAdded event.
Don't Test Yet

This script won’t run properly until it’s called by PlayerManager, which will be shown in the next section.

Connect Modules and Test

With the PlayerManager created, require it so that the code in that module script can then run and send players to the lobby.

  1. Go back to MatchManager and create variables for the following:
  • ServerStorage service.
  • ModuleScripts folder, child of ServerStorage.
  • Loading the PlayerManager module script, child of moduleScripts.
  1. Use a local server with at least the minimum players to test:
  • All players spawn in the Lobby.
  • The print statement from PlayerManager appears in the Output Window.

Once finished, click Cleanup to shut down the server.

  • Check the name of parts such as the Arena, or the location of Lobby > StartSpawn, especially if you named them differently than instructed in the lesson.
  • Make sure that modules are required and correctly spelled.

Sending Players to the Arena

Now that players spawn in the lobby, teleport them into a match once the intermission is over. Change the player’s RespawnLocation to a spawn location in the arena using a function in the Player object called ReloadCharacter().

  1. Go to the PlayerManager script, below onPlayerJoin(), add a new local function named preparePlayer(). Include two parameters: player and whichSpawn, the spawn location to send them to.
  1. Set the player’s respawn property to whichSpawn.
  1. Force the character to reload, using LoadCharacter(), and the player will respawn using their newly assigned location.
LoadCharacter() Limitations

LoadCharacter() creates a new character, or avatar, for a player. In doing that, it clears the player’s current backpack and any tools inside. This is useful for keeping players from sneaking tools in. Any items you want the players to have will have to be placed back into the backpack after reloading.

Code a For Loop with Pairs()

Make sure each player gets teleported to a different spawn location in the arena by combining a for loop and the pairs function to go through the active players array.

pairs() will go through every value in an array without needing to know the size of the array, or starting and ending points. Compare going through an entire array with and without pairs() below.

With Pairs
local numberArray = {"one", "two", "three", "four"}

for count, currentNumber in pairs(numberArray) do
Without Pairs
local numberArray = {"one", "two", "three", "four"}

for count = 1, #numberArray, 1 do
   local currentNumber = numberArray[count]
  1. In the sendPlayersToMatch() function, use a variable to create an array of all the arena spawn locations by getting the children of the Arena > SpawnLocations folder.
  1. Add the for loop below to get an array of all players and then iterate through each of them. To get players, use:

Track and Spawn Players

Keep track of who’s currently in the arena by using the for loop to add the name of each player into the activePlayers array. That array will be used for code that you only want to apply to players currently in a match.

  1. In the for loop, use table.insert(). Because insert() takes two parameters, the table to add into, and what to insert, use activePlayers so you can insert whichPlayer. That way, each player will be added to the table each time the loop runs.
  1. To get a spawn location from the arena, create a variable named spawnLocation and set it to the first index in the arenaSpawns table.
  1. Call preparePlayer() and pass in whichPlayer and spawnLocation. Then, since that spawn location was used, remove it from the table so the next player will get a different spawn.
  1. Test on a local server that players are sent to the arena. The players will continue to respawn at the same location because the code to send them back to the lobby isn’t yet in place.
  • When using GetPlayers(), make sure there are two closing parentheses, such as Players:GetPlayers()) in the statement.
  • Check the series of function calls in the module scripts. For example, matchManager.prepareGame() should call playerManager.sendPlayersToMatch().

Giving Players Weapons

In the arena, players will get the same tool to use as a melee weapon. The template for the tool will be stored in ServerStorage so copies can be provided for player.

Set up a Tool in ServerStorage

  1. Download the premade sword, or create your own (see Intro to Tools).
  1. In the Explorer, place a tool named Weapon in Workspace > ServerStorage.
If using the premade weapon, import it by right-clicking on ServerStorage > Insert From File > select Weapon.rbxm.

Load Characters with Tools

  1. In PlayerManager, in the Player Variables section, add a variable named playerWeapon for the Weapon in ServerStorage.
  1. In preparePlayer(), paste the following code to get the player’s character.
Why Use Wait()?

It’s possible that a player’s character isn’t loaded when the game starts. Adding the extra wait ensures that if a character isn’t immediately available, the script will wait and not cause an error.

  1. Create a new variable named sword and use the Clone() function to create a copy of the Weapon in ServerStorage. Then, parent the sword to the player’s character.
  1. Test on a local server to confirm that every player gets a tool when sent to the arena.
Keep in mind, if you continue testing, the intermission will keep restarting and so players will respawn every few seconds. This will be resolved in the next lesson.

Make sure that the Weapon is in ServerStorage and correctly spelled in the script.

Finished Project Sample

Project File

Download the finished project.


Previous Coding the Game Loop Next Timers and Custom Events