title | description | next | prev |
---|---|---|---|
If/then Practice with Traps |
Create a trap that sets the player's health to zero with Roblox Lua. |
/tutorials/fundamentals/coding-3/powerups-with-if-statements |
/tutorials/fundamentals/coding-3/intro-to-if-statements |
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.
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 0 local trapPart = script.Parent
-
Create a function named
onTouch()
with a parameter namedotherPart
.-- If a player touches this part, set their health to 0 local trapPart = script.Parent local function onTouch(otherPart) end
-
Connect the function to the trap part's
Touched
event to run whenever something touches the part.local trapPart = script.Parent local function onTouch(otherPart) end trapPart.Touched:Connect(onTouch)
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.
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()
, typelocal character = otherPart.Parent
.local trapPart = script.Parent local function onTouch(otherPart) -- Finds otherPart's parent object local character = otherPart.Parent end trapPart.Touched:Connect(onTouch)
-
Check to see if
character
has aClass.Humanoid
by typing:local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local trapPart = script.Parent local function onTouch(otherPart) local character = otherPart.Parent local humanoid = character:FindFirstChildWhichIsA("Humanoid") end trapPart.Touched:Connect(onTouch)
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.Parent local function onTouch(otherPart) local character = otherPart.Parent local humanoid = character:FindFirstChildWhichIsA("Humanoid") -- Evaluates if a Humanoid was found if humanoid then end end trapPart.Touched:Connect(onTouch)
-
Add a print statement and check the code so far.
local trapPart = script.Parent local function onTouch(otherPart) local character = otherPart.Parent local humanoid = character:FindFirstChildWhichIsA("Humanoid") -- Evaluates if a Humanoid was found if humanoid then print("Found a Humanoid") end end trapPart.Touched:Connect(onTouch)
-
Run the code and check that you can see the output whenever a player touches the part.
If the statement is true, you can use the same humanoid variable to set the player's health to 0.
-
Between
then
andend
, typehumanoid.Health = 0
.local trapPart = script.Parent local function onTouch(otherPart) local character = otherPart.Parent local humanoid = character:FindFirstChildWhichIsA("Humanoid") -- Evaluates if a Humanoid was found if humanoid then print("Found a Humanoid") humanoid.Health = 0 end end trapPart.Touched:Connect(onTouch)
-
Test the trap.
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.