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

Time to code the last phase of the game: cleanup and reset. Code in this phase ensures that the game loops to intermission and that future matches start the same for each player.

Intermission
Competition
Cleanup and Reset

Updating the GUI

Before doing cleanup and reset, inform players how the game ended using the DisplayManager to show the appropriate status.

Get the Winning Player Name

Start by getting the winning player’s name if there was one. Previously, the code checked if the size of activePlayers was down to 1. To get the remaining player’s name, return the name at the first index of that table.

  1. In PlayerManager, start a new module function named getWinnerName().
  1. Add an if statement that runs if something exists in activePlayers[1].
  • Although the table count was checked before, the player might have disconnected or left the game.
  1. In the if statement:
  • Store the winning player in a variable.
  • Return the player's Name property.
  • Add an else statement that returns a string noting an error happened in the event the player has left.

Get the End Status

Use a module function to take information from the correct end state, whether it’s TimerUp or FoundWinner. Then, send that state variable to DisplayManager to update the status GUI with the appropriate message.

  1. In MatchManager, code a new module function named getEndStatus() with a parameter named endState. To store the message that will be sent, add an empty variable named statusToReturn.
  1. Set the value of statusToReturn using if and elseif statements. Check for the end state variables: FoundWinner and TimerUp. For error checking, include an else at the end.
  • To get a variable, type gameSettings.endStates followed by a dot and appropriate variable name.
  1. Add the following for each statement:
FoundWinner
  • A variable for the winner using playerManager.getWinnerName().
  • Update statusToReturn with a string announcing the winner.
TimerUp
  • Update statusToReturn with a string announcing time ran out.
Else
  • Update statusToReturn with an error message in case there are issues with getting the end game message.
  1. Send back the message by typing return statusToReturn.

Display and Test

Get the updated announcement from statusToReturn in GameManager and display it to the players using the DisplayManager.

  1. Open GameManager. In the while true do loop, delete the last print statement.
  • Instead of the print, create a variable named endStatus and call matchManager.getEndStatus().
  • Pass in endState the variable indicating how the game ended.
  1. To display the returned message in the GUI label, call displayManager.updateStatus() and pass in endStatus.
  1. So the game pauses to let players see the message, add a wait using transitionTime.
  1. Start a test server and check that players see the following messages:
  • Time is up: "Timer ran out!"
  • One player is left: "Winner is [Player Name]"
Time is up condition
Winning player condition

Remember, you can eliminate players one at a time using the command bar. In the server window’s command bar type: workspace.PlayerName.Humanoid.Health = 0


  • If your end status message is "Error Found", none of the conditions were successful. Check the code in MatchManager.getEndStatus() against the code samples.
  • If the end status doesn’t display, check that wait(gameSettings.transitionTime) is after sending the message to displayManager.

Starting New Matches

Before starting a new match, there will be a brief transition. This gives players time to see the end status and makes being teleported to the lobby feel less sudden.

At the end of the transition, remaining players will be removed from the arena, and all of the code will reset. This ensures that players will start the next match with a clean version of the game.

Transition to Intermission

So players don’t fight during the transition, remove their weapons.

  1. In PlayerManager, find the local functions. Copy and paste the highlighted code for removePlayerWeapon() below. The code will remove an individual player’s weapon if it’s actively equipt or in the player’s backpack.
  1. Code a new module function named removeAllWeapons().
  • This function will run removePlayerWeapon() for each player.
  1. In removeAllWeapons(), use a for loop with pairs() to go through activePlayers table.
  • In the loop, call removePlayerWeapon() and pass in the player found.

Cleanup

Cleanup will be its own function in MatchManager. For now, cleanup will just use that previously created function to remove player weapons. As you expand the game, more can be added, such as functions to reset a map that changed during a match.

  1. Open MatchManager. Add a new module function named cleanupMatch().
  • In that function, call playerManager.removeAllWeapons().
  1. Next, call the cleanup function. Open GameManager and find the while true do loop.
So players have weapons removed during the ending intermission, call matchManager.cleanupMatch() before the last wait() .
  1. Start a test server and run a match. Wait for the timer to run out and confirm that the player’s weapon is removed during the end game intermission.

Note both the weapon and icon at the bottom were removed.

Reset

You may have noticed a few other things in the game, like players still being in the arena after a match ends. With the match cleaned up, next reset the game. This includes sending players in the arena back into the lobby and clearing the active players table. With a reset in place, a game loop can run indefinitely.

First, start a function to send players back to the lobby.

  1. In PlayerManager:
  • Create a module function named resetPlayers().
  • Add a for loop with pairs() to iterate through activePlayers
  • In the loop, call respawnPlayerInLobby() and pass in the player as the parameter.
  1. Make sure the activePlayers table is empty for the next match by setting it to {}.
  1. Open MatchManager.
Code a new module function named resetMatch() and call playerManager.resetPlayers().
  1. Go back to GameManager. At the end of the while true do loop, call:
matchManager.resetMatch()
  1. Start a test server and run a match. Confirm that you can at least go through two game loops without any errors.

Now that the game loop is complete, continue to the next lesson and learn ways to improve and polish gameplay, like using a prop pack to improve the game’s world.

Tracking Down Errors
If a function has an error, or you get a nil (non-existent value) error, you may need to track down the start of that function. Then, trace the series of steps in running that function, confirming everything is correctly typed. For example, if you notice players don’t have weapons removed after a match, follow this series of steps to check:

  • (Start) GameManager > matchManager.cleanupMatch()
  • MatchManager > playerManager.removeAllWeapons()

Using the Lua Debugger
If an output message isn’t clear, or you want to see exactly where a code stops working, check out documentation on using the Lua Debugger.


Finished Scripts

Project File

Download the finished project.

Scripts