How to Add a Second Player to Your Minecraft Game
After Iteration 1 you can move on to Iteration 2 and add in another player so that your Minecraft game becomes a multiplayer game. To add Player2, follow these simple steps:
Start with main and look at the first function, SetupMultiplayer. Add another variable named Player2, and put the Minecraft username of the second player there.
Here’s an example where the two players are thoughtstem and thoughtstem2.
It’s difficult to test the multiplayer version of your game without having a real-life second player to join you, so work with a friend who also has Minecraft and who can join your server when you’re ready to test.
Back in main, the next function is arena. The arena function doesn’t need to know what players are playing, so you can skip to the next line.
The third line in main is the event that calls the StartGame function when a block is broken.
In the StartGame function, make sure both players are set to Survival mode. Also, because you don’t know which player broke the melon block, make sure both players are teleported to the center of the arena.
This shows how to make those changes.
Still in the StartGame function, rename the blocksDestroyed variable to become more specific to Player1, and then add another variable for blocks destroyed for Player2.
Back in main, the next line is the event that is called whenever a player respawns.
The SetupPlayer is called. To make SetupPlayer multiplayer-safe, add an else-if statement to the if statement that essentially does the same as Player1, but for Player2.
Add a second message to let each player know how many blocks the other player has broken.
Here are the changes to the SetupPlayer function.
Be sure to use all the right variables for Player1 and Player2 or else when Player1 respawns, it will teleport Player2 back to the arena.
Back in main, the next line is the event that is called when a player moves; the removeBlockAfterStep function is called. The image below shows the change to the if statement that takes place. If either Player1 or Player2 was the one who moved, call the removeBlock function.
Change the removeBlock function also, to increment the correct blocksDestroyed counter and to announce the name of the 200-block destroyer.
This is how to change this if statement.
You’re done! Here are the final batches of code.
You can also check it out at mod.learntomod.com.