Cleanup and Reset
Cleanup and Reset
Students will be able to:
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.
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.
- In PlayerManager, start a new module function named
- Add an if statement that runs if something exists in
- Although the table count was checked before, the player might have disconnected or left the game.
- In the if statement:
- Store the winning player in a variable.
- Return the player's
- 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
FoundWinner. Then, send that state variable to DisplayManager to update the status GUI with the appropriate message.
- 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
- Set the value of
statusToReturnusing if and elseif statements. Check for the end state variables:
TimerUp. For error checking, include an else at the end.
- To get a variable, type
gameSettings.endStatesfollowed by a dot and appropriate variable name.
- Add the following for each statement:
- A variable for the winner using
statusToReturnwith a string announcing the winner.
statusToReturnwith a string announcing time ran out.
statusToReturnwith an error message in case there are issues with getting the end game message.
- Send back the message by typing
Display and Test
Get the updated announcement from
statusToReturn in GameManager and display it to the players using the DisplayManager.
- Open GameManager. In the
while true doloop, delete the last print statement.
- Instead of the print, create a variable named
- Pass in
endStatethe variable indicating how the game ended.
- To display the returned message in the GUI label, call
displayManager.updateStatus()and pass in
- So the game pauses to let players see the message, add a wait using
- 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]"
Quickly Test For FoundWinner »
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
Troubleshooting Tips »
- 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
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.
- 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.
- Code a new module function named
- This function will run
removePlayerWeapon()for each player.
removeAllWeapons(), use a for loop with
pairs()to go through
- In the loop, call
removePlayerWeapon()and pass in the player found.
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.
- Open MatchManager. Add a new module function named
- In that function, call
- Next, call the cleanup function. Open GameManager and find the
while true doloop.
matchManager.cleanupMatch()before the last
- 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.
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.
- In PlayerManager:
- Create a module function named
- Add a for loop with
pairs()to iterate through
- In the loop, call
respawnPlayerInLobby()and pass in the player as the parameter.
- Make sure the
activePlayerstable is empty for the next match by setting it to
- Open MatchManager.
- Go back to GameManager. At the end of the
while true doloop, call:
- 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.
General Troubleshooting Tips »
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 >
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.
Download the finished project.
Previous Ending Matches Next Get the Game Ready