The Haunted Library Part 5: Finishing the Escape Room

Step 1
Follow the clues!

In this lesson, we will be making the Escape Room happen! We will use the functions we made to spawn entities, show levers and buttons, and fill inventories, and pull that together into the sequence players have to follow in order to escape.

Step 2
Save yourself some time!

To keep from having to solve the Escape Room every time you want to test something, modify these two exports functions.

/js secretPassage() will just open the Secret Passage now, so that you can easily get in and out.

/js testInventory() will just give you the key, so that you can always get out of the library.


P.S. Using world.dropItem() is a super easy way to get your custom item if you don't want to make a crafting recipe!

Step 3
Find the opposite corner of the library

To tell if a player is inside the library, we will need the coordinates of the opposite corner from our starting corner. Move into the back right corner of the library, and break a block to get the location of the corner just below the ceiling.

Step 4
Save the second corner

Add a property called corner2 to your library.interior object. Yaw and pitch won't matter, since we are only comparing the X, Y, and Z to see if a player is inside.

Step 5
Find the player's starting location

We need to choose a point to teleport all the players to when the Escape Room starts. Move to the entrance, and record the position of where you are standing.

You will want to save your Yaw and Pitch this time, to make the players look up at the spooky portrait.

Step 6
Save the starting position
Step 7
Make a function to determine if something is inside a room

This function will take an entity (like a player, custom Mob, even a block) and tell you true or false on whether not it is between the corner locations you give it for corner1 and corner2.

Step 8
Check if a player is in the library

Test the new function by checking if a player is in the library each time someone moves on the server.

Check Your Work
Step 9
Are you inside the library?

Save, deploy, reload and move around. You should see "Someone is in the library" if you are inside of it, and you should stop seeing that message if you go outside.

Step 10
Add started and finished properties to the library
Step 11
Make a method to start the library
Step 12
Make the library start automatically if anyone walks in
Check Your Work
Step 13
Test out your library start.

Save, deploy, reload and move around inside the library. You should be teleported to the entrance, looking up at the portrait.

If an instructor is available to connect to your server, they should be teleported into the library as well.

Step 14
Clear out items and entities

This code will put all of the players in Adventure Mode, show the title of the Haunted Library, and kill all entities.

You may want to keep the inventory clear and gamemode commented, so that you don't get pulled out of Creative and lose all your items every time you reload

Check Your Work
Step 15
Test the start again

Save, deploy, and reload. As soon as you are inside the library, it should start, and kill any entities in the area.


You may notice that hanging entities, like paintings and item frames, also get killed by this command! We will add them back in the next step.

Step 16
Place the portrait and filing cabinet drawers

Add the portrait and filing cabinet drawers back in by calling the methods you made after the console command to kill all entities except players.

Check Your Work
Step 17
Test the start again

Save, deploy, reload, and make sure your item frames and paintings are appearing

Step 18
Cancel hanging entity breaks

Adventure mode will keep our players from being able to break blocks, but for some reason they can still break hanging entities, like item frames and paintings. We will add this event function for the hangingBreakByEntity event to cancel any breaks while the library is going.

Step 19
Place the office supplies when a player finds the Restricted Section

The if statement here cancels the event if the player tries to take anything except "The Secrets of the Library" out of the Restricted Shelf.

That means that our else will happen when they do click the correct item. That is when we will fill the filing cabinet with office supplies.

Step 20
Add a sign

To make sure players notice that the filing cabinet has changed, we're going to use the drone to add a sign when we fill the filing cabinet.

Check Your Work
Step 21
Test out the sign and office supplies

Save, deploy, reload, and go get "The Secrets of the Library" out of the Restricted Shelf. The sign should appear over your filing cabinet, and the bottom one should have paper, ink, and a book and quill in it.

Step 22
Add some spooky music and bats

Use a for loop to spawn some bats when the library starts, and play the creepy record11.

Step 23
Make the library dusty!

Borrowing some skills we learned in the Custom Particle Trails lesson, make a small smoke trail follow anyone while they are in the library.

Step 24
Find the opposite corner of the secret room

This will be the ceiling block directly above the corner where your pressure plate goes.

Step 25
Save the second corner and give the room an "entered" property
Step 26
Lock the player in

We will add this code. It checks if no one has entered the secret room yet, since we only want it to lock the first time we go in.

Check Your Work
Step 27
Test the closing passage

Save, deploy, reload, and make sure the secret passage closes once you enter the secret room.

Step 28
Start the library if even if you start in the basement

Right now, a problem with our code is that you can't get out of the hidden basement unless the library has started, but you do get locked in. We'll use an if and else statement to teleport you to the start if you are down there before the library has started.

Step 29
Place the hidden cookies

Call your function that fills Melvil's secret stash of cookies in the chest under the desk, but not until a player clicks the enchanting table in the Secret Room.

Check Your Work
Step 30
Test out the secret stash

Before you do anything, check the chest under the desk to make sure that it is empty. Save, deploy, reload, and go get Melvil's diary out of the basement. When you get back upstairs, the desk chest should be full of cookies.

Step 31
Duplicate ghosts. . .

Right now, our code will spawn a Melvil ghost every time we step on the pressure plate in the secret room. In the next few steps, we will be fixing this bug.

Step 32
Make a variable to tell if the ghost has spawned
Step 33
Make the variable true when you spawn the ghost
Step 34
Only spawn the ghost if one hasn't spawned yet.
Step 35
Find the locations for the doors

Because we have two doors, and each door is two blocks, to keep track of when a player tries to open the door, we need to save 4 locations.

Step 36
Save the locations

Give the library object a property called doors and assign it to an array of the four door location blocks.

Step 37
Use a loop to check all four door blocks

Because we have four different blocks for the doors, we need to use a loop to check them all inside of our playerInteract event function. If the player is holding the key to the library, they will be able to open the door, and escape the escape room.

Otherwise, the door should slam closed (because the event that should have opened it gets canceled) and make a thudding noise.

Check Your Work
Step 38
Test the doors

Save, deploy, reload, and try to open the doors. Make sure you have headphones to test the sound as well.

Then, give yourself the library key with /js testInventory() and try opening the door again. You should be able to get out.

Step 39
Rebuild the doors when the library starts

To keep a door from being stuck open when you first walk in, we build new doors in the same place. Here, we use library.doors[0] and library.doors[2], because they were the two bottom blocks.

You can tell by which blocks have the lower Y

Step 40
Find a location outside of the library
Step 41
Make a method to reset the library
Step 42
Make a method to finish the library that calls your library reset method
Step 43
Call the library finish method when the library is finished
Step 44
You finished the library!

Congratulations, you have made the entire Haunted Library Escape Room! If you can, grab some friends and have them try to solve the puzzle! Or celebrate by blowing it to smithereens with a Nuclear Bow. We can always use the drone to build it back again.


The final lesson of this course is a completely new challenge that will test what you learned in the previous lessons. We will break it down into individual challenges, worth a grand total of 1000 points!

The Haunted Library Part 5: Finishing the Escape Room Info


MVCode Clubs

Created By

Mandala2 slinkous


The Haunted Library

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach