Imported Code

The template imported in the last lesson had Lua scripts included. This lesson will be creating a sample script to see how this code works and will be useful in the future.

Imported Scripts

The scripts imported can be seen in the Project Content window. There are three scripts to operate the doors and one script to toggle a VFX for the Treasure object in the 2nd puzzle room.

Start Door

The Start Door is a basic door found in the Core Content window. It will open and close based on the player’s proximity to the door. This uses the BasicDoorControllerServer and BasicDoorControllerClient scripts.

Puzzle Doors

There are three Puzzle Doors for each room. They are a modified version of the default door that will only open when a specific server event is broadcasted. This will be useful to only open the door once the player has solved the riddle. It uses the PuzzleDoorControllerServer script.

Treasure VFX

The PlaySparkle script can receive two client events to enable and disable a Sparkle effect on the Treasure object in the second puzzle room. This will be useful for the second puzzle that requires the player to know if it is looking at the Treasure directly.

Create a New Script

In the Hierarchy window, create a new folder named Scripts. Create a Server Context inside the Scripts folder. Create a new script named ImportedScriptsTest and place it in the Server Context.

Add a Custom Property

Select the ImportedScriptsTest and open the Properties window. At the bottom, click the Add Custom Property button. Select the Bool type and name it RunTest. This will be used to indicate whether or not we want the script to run. Click the checkbox next to the RunTest property to activate it.

Open the Puzzle Doors

Add the following code to the ImportedScriptsTest script.

local RUN_TEST = script:GetCustomProperty("RunTest") if not RUN_TEST then return end Task.Wait(3) Events.Broadcast("OpenPuzzleDoor", "1") Task.Wait(3) Events.Broadcast("OpenPuzzleDoor", "2") Task.Wait(1) Events.BroadcastToAllPlayers("PlaySparkle") Task.Wait(1) Events.BroadcastToAllPlayers("StopSparkle") Task.Wait(1) Events.Broadcast("OpenPuzzleDoor", "3")
Code language: Lua (lua)

Opening specific Puzzle Doors

When broadcasting the OpenPuzzleDoor event, it requires a second parameter of a string to control which door is being opened. This string is being compared to an id custom property on each of the Puzzle Door objects.

Preview the Project

Press the Play button and check if the doors open in the correct order and the Treasure Sparkle effect plays.

Finishing Up

Now that the imported code functionality is clear, deactivate the custom property RunTest on the script.

Post a comment

Leave a Comment

Scroll to Top