Traps that decrease players' health are a fun game-play element that can be coded with conditional statements. Practice using conditionals by creating a part that sets the player's health to zero when touched.
Setting up the Trap
Traps work exceptionally well in experiences with movement-based challenges, like obbies. These steps will start by setting up the necessary variables and functions. Do as much as you can without looking at the code boxes first.
Create and name a trap part. Insert a script into the part.
In the script, add a descriptive comment and then use a variable to reference the script's parent.
-- If a player touches this part, set their health to 0local trapPart = script.ParentCreate a function named onTouch() with a parameter named otherPart.
-- If a player touches this part, set their health to 0local trapPart = script.Parentlocal function onTouch(otherPart)endConnect the function to the trap part's Touched event to run whenever something touches the part.
local trapPart = script.Parentlocal function onTouch(otherPart)endtrapPart.Touched:Connect(onTouch)
Checking for Player Touch
Remember, the parameter otherPart records whatever touches the trap part, which might be a part of a player or just the baseplate.
To ensure the trap will only destroy players and won't destroy random decor items, use an if/then statement to check if whatever is in otherPart contains a Humanoid object.
Finding a Specific Object
The function FindFirstChildWhichIsA() can be used to look for specific object types, which is handy because we're looking for a Humanoid-type object. Players will likely touch the trap with only a part of their avatar, so a variable must be set up to find the parent of the touching part and search it for a Humanoid.
In onTouch(), type local character = otherPart.Parent.
local trapPart = script.Parentlocal function onTouch(otherPart)-- Finds otherPart's parent objectlocal character = otherPart.ParentendtrapPart.Touched:Connect(onTouch)Check to see if character has a Humanoid by typing:
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")endtrapPart.Touched:Connect(onTouch)
Checking with an if Statement
If a Humanoid is found, then set the Humanoid's Health to zero.
Use an if statement to check if a Humanoid was successfully assigned to local humanoid.
local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")-- Evaluates if a Humanoid was foundif humanoid thenendendtrapPart.Touched:Connect(onTouch)Add a print statement and check the code so far.
local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")-- Evaluates if a Humanoid was foundif humanoid thenprint("Found a Humanoid")endendtrapPart.Touched:Connect(onTouch)Run the code and check that you can see the output whenever a player touches the part.
Changing the Player's Health
If the statement is true, you can use the same humanoid variable to set the player's health to 0.
Between then and end, type humanoid.Health = 0.
Completed scriptlocal trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")-- Evaluates if a Humanoid was foundif humanoid thenprint("Found a Humanoid")humanoid.Health = 0endendtrapPart.Touched:Connect(onTouch)Test the trap.
Summary
This trap part used conditionals to detect Humanoid parts and set the Humanoid's health to zero. This script is an improvement on the previous trap script, which destroyed any touching object no matter what it was.
It does, however, still have a few flaws. Humanoids aren't just in players. Humanoids are also found in non-playable characters. The script is also only good at setting the player's health to zero. You can experiment with subtracting a small amount of health, but it's likely to subtract the health faster than desired. Later lessons provide further improvements to give greater control over how much health is subtracted from players.