UevKF8zjRV_WEtpJsXhr
We use cookies on this site to enhance your user experience

Parameters and Events 1: Traps

10 min

Parameters and Events 1: Traps

Trap parts harm players, and possibly even other objects they touch. Adding trap parts to your game makes it more challenging for players to survive. This script will create a trap part that destroys whatever touches the part, including other parts. Be careful to anchor the trap where it doesn’t fall and destroy things unintentionally.

touchPartsFinalExample.gif

Setting up the Project

Create a new Part

  1. Create a new part that’s not touching anything.
  2. In the Explorer, rename the part:TrapPart.
  3. Anchor the part.
Make Sure the Part is Anchored and Floating

If not, it may touch something and unintentionally destroy it.


Create a Script and Variable

  1. Add a new script into the part.
  2. Rename the script TrapScript.
  3. Replace Hello World with a descriptive comment.
  4. Under the comment, type local trap = script.Parent

Your code should now be:

Creating Parameters for Functions

The trap will use a function to destroys whatever touched the part. To work, the function needs to know what touched the part.

To take information in from the outside, functions use placeholders called parameters. Parameters are typed inside the () that comes after a function’s name. Parameters are also variables, so they can be named anything you want.

Create a Function with a Parameter

You’ll create a new function called onTouch() with a parameter called objectTouched that will track whatever touches the trap and then destroy the touching part.

  1. Create a local function. While your function can be named anything, this lesson will use onTouch

    trapPart_02.png

  2. Inside the (), type a name for the parameter. This lesson will use objectTouched

    trapPart_03.png

Naming Rules - Parameters

Because parameters are variables, the first letter is lowercase and every next word is capitalized.

Examples: playerItem, objectTouched, player


  1. Between local function onTouch() and end, create a print statement to help you test the function so far.

    trapPart_04.png

You’ll use this to check if something is touching the part in the next section.

Using Events to Call Functions

We want the function to run whenever something touches the part. To make that happen, connect the function to the Touched event. Events are things that happen in-game. Like a player touching a part or losing health. When a function is connected to an event, the function run whenever the event happens.

The Touched event fires whenever one part touches another part and can be used to create buttons, traps, and other objects that players interact with.

Create a Touched Event

  1. Under the function’s end, not inside the function, type trap.Touched:Connect(onTouch)

    trapPart_05.png

  2. Run Studio, touch the part, and check for your test print statement.

    testTouchingPart.png

Troubleshooting Tips

If you can’t see the string in the Output Window, check the following:

  • Make sure the string "Something touched the part!" is between local function onTouch() and end.
  • Check that Touched is capitalized

Destroy the Part

Now that the function is correctly set up, use the function to destroy whatever touches the part.

  1. In the function after the string, type objectTouched:Destroy()

    trapPart_07.png

  2. Test your script and see what happens when the player touches the trap now.

    touchPartsFinalExample.gif

Using the : Symbol

: is a special symbol that connects an object to a function. In this case, Destroy() will be used on whatever object is stored inside the objectTouched parameter.


Your code should now be:

Troubleshooting Code

Issue: There’s an unexpected problem when starting up, like part of your level has been destroyed or the screen flashes.

  • Make sure that the trap part is anchored. If not, it may accidentally drop and touch something.
  • Make sure that the part isn’t touching anything as you start the game.

Troubleshooting Tips:

  • Touched is capitalized.
  • part.Touched:Connect(onTouch) should come after the custom function.

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.