Lava Floor
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 12 of 25.
From the touch pad you know that hit is a body part. Its Parent is the character, the model holding all the body parts. The character also holds a Humanoid, which owns Health.
FindFirstChild looks for a child by name and gives back nil (nothing) when there is none:
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
print(humanoid.Health)
endThe if skips its block when humanoid is nil. A falling ball has no Humanoid, and using nil as if it were a Humanoid raises an error in Output every time something that is not a player touches the part.
A Humanoid whose Health reaches 0 is defeated, and the player respawns a few seconds later.
Challenge
MediumTurn the glowing orange strip into lava.
- Inside the Touched function, look for a
Humanoidinhit.ParentwithFindFirstChild. - If there is one, set its
Healthto0. A check drops a ball on the lava: it has no Humanoid and must cause no error.
Press Play and walk into the lava: you are defeated and come back 5 seconds later. Then Check.
Try it yourself
local lava = script.Parent
lava.Touched:Connect(function(hit)
end)
All lessons in Roblox Game Scripting
2Loops and Functions
Row of PillarsStaircase BuilderTower BuilderCoin FactoryBlinking LightVanishing Bridge5Your First Game
Your First ObbyPractice on your own: Online Lua compiler