Parameters and Events 2: Buttons

Parameters and Events 2: Buttons

Buttons can be used by players to unlock new areas, give points, and reveal treasure. Whenever a player touches a button, it should give some feedback to let know players it’s being interacted with, like change colors or make sounds.

In this example, the button will be used to activate a bridge. When the button has been pressed, it’ll turn green and players will be able to use the bridge. If the player hasn’t pressed the button, they’ll fall through the bridge when they try to walk on it.


Setting up the Parts

  1. Create an area in your game where players will need a bridge.
  2. Create a part for the bridge and rename it Bridge.
  3. Select the bridge, in the Properties window:
    • Change Transparency to .5. For Transparency, 0 is visible, 1 is invisible.
    • Check Anchor.
    • Uncheck CanCollide.
  4. Test the game, the bridge should be misty looking and you shouldn’t be able to walk on it.


Create the Button

Now that the bridge is setup, create the button.

  1. Create a new part named Button.
  2. Change the button color to red.
  3. Anchor the button.
  4. Move the button so it’s slightly floating and not touching anything.


Making a Button Interactive

This time, instead of using the Touched event to create a trap, you’ll use it to create a button that makes the bridge usable.

Create the Script

To make the bridge collidable, use the code bridge.CanCollide = true within a custom function that runs when a player touches the button. You know everything else you need to complete the following steps.

Try to figure it out yourself before looking at the code solution.

  1. Insert a script into the button named ActivateBridge.
  2. Delete Hello World in your new script.
  3. Create variables for the bridge and for the button.
  4. Create a custom function that does the following the button is touched:
    • Prints “button touched”
    • Changes button’s color to green
    • Changes bridge’s transparency to 0 to make it visible
    • Makes the bridge usable by using the code bridge.CanCollide = true
  5. Connect the function to the button’s Touched event.

Test the Script

  1. Play the game and see if players can use the bridge after touching the button.


Troubleshooting Your Code

Issue: The bridge is already solid when the game starts.

  • Make sure that the part are Anchored and not touching anything. The parts might touch something, like terrain or another part, and cause the buttonPressed() function to fire accidentally.

Troubleshooting Tips

  • If you get an error in the Output Window saying: "Bridge is not a valid member of Workspace", check the naming of your bridge. The Bridge in your script must be named exactly like in the Explorer.

  • Check that part.Touched:Connect(buttonPressed) is outside the buttonPressed() function.

Optional Code Challenge

The script in this lesson can also be used to keep doors that keep players out of specific areas. Practice your coding skills by making these changes to the script:

  1. Create a door part.
  2. Create a button further away from the door.
  3. Change the script in this lesson so it makes the door change transparency and so the player can’t collide with it.

Your version might look something like this:

These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.

Previous Parameters and Events 1: Traps Next Multiple Parameters and Arguments