Traps with if/then Statements
Traps with if/then Statements
With if/then statements, you can make a trap that changes a player’s health rather than just destroying everything it touches. Roblox avatars contain an object called a humanoid that lets players move around and also controls player health. An if/then statement can be used to check if whatever is touching the trap has a humanoid part. If it does, then the player’s health will be set to 0.
Setup the Part and Script
Insert and name a trap part.
In the trap part, insert and name a script.
Create a variable referencing the script’s parent part:
Create a Custom Function
Create a new function that will connected to the trap part’s Touched event.
Create a function with a parameter. This example will name them
Inside of the function, create a variable to store the parameter’s parent object.
More About Humanoids
A Humanoid object, just like a real life human, is made of many parts like arms and legs. In game, only a specific part of the Humanoid, like their leg, will touch the trap.
To change the Humanoid’s health, the script needs to get the owner, or parent, of that leg.
otherPart.Parent is then a way to store the part’s parent so it can be used later.
Connect the function to the trap part’s
Touchedevent so that it will run whenever something touches the part.
Check Work with Print Statements
You can add in a print statement to check if your code is working.
If the code works, you’ll notice the print text appear in the Output Window whenever a part or another player touch it.
Checking for Humanoids
Remember, the parameter
otherPart records whatever touches the trap part, but that might be part of a player or it might be your baseplate. To make it is so the trap will only destroy humanoids, use an if/then statement to check if whatever is in
otherPart contains a Humanoid object.
Finding a Specific Object
FindFirstChildWhichIsA() can be used to look for specific types of objects, which is handy because we’re looking for a Humanoid type object. You can use a variable to store whatever Humanoid is found.
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
Watch Out For Variable Names
If you named your last variable something other than “character”, make sure that you use that name instead.
Add an if/then to Find Humanoids
Use an if/then statement to check if there is anything being stored inside of the humanoid variable you just made.
After local humanoid, create an if/then statement with the condition
if humanoid then
Understanding if humanoid then
If humanoid then only checks to see if there’s anything in the variable humanoid.
If no Humanoid object was found when something touched the part, the humanoid variable will stay empty, and the if/then statement will stay false. If something is stored in humanoid, then the statement becomes true.
Change the Humanoid Health
If the statement is true, you can use the same humanoid variable to set the player’s health to 0.
humanoid.Health = 0
Don't Use Destroy on Humanoids
character will not actually destroy the player but cause the game to break since that player won’t respawn properly. Always set a player’s health to 0 if you want them to restart their game.
Test your game.
Finished Code Example
NPCs Are Also Humanoids
NPCs, or Non-Playable Characters, also have a humanoid. That means this code will affect NPCs as well.
Final Project Example
- Create a part that will turn red and fall after one second if a player steps on it.
- Create a part that sets the player’s health to 100 instead of 0.
- Fire instances don’t automatically do damage. Create a fire pit that will harm any players that touch it.
Troubleshooting Your Code
- Check that all of your variables match. They all should have the same spelling and capitalization.
- You should have two
ends. One for the
if/thenstatement, and one for the 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.