Parameters and Events 2: Buttons
Parameters and Events 2: Buttons
Students will be able to:
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
- Create an area in your game where players will need a bridge.
- Create a part for the bridge and rename it Bridge.
- Select the bridge, in the Properties window:
- Change Transparency to .5. For Transparency, 0 is visible, 1 is invisible.
- Check Anchor.
- Uncheck CanCollide.
- 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.
- Create a new part named Button.
- Change the button color to red.
- Anchor the button.
- 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.
- Insert a script into the button named
Hello Worldin your new script.
- Create variables for the bridge and for the button.
- 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
- Connect the function to the button’s
Test the Script
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.
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.
part.Touched:Connect(buttonPressed)is outside the
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:
- Create a door part.
- Create a button further away from the door.
- 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.