Menu
CoddyTech

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)
end

The 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 icon

Challenge

Medium

Turn the glowing orange strip into lava.

  1. Inside the Touched function, look for a Humanoid in hit.Parent with FindFirstChild.
  2. If there is one, set its Health to 0. 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

Practice on your own: Online Lua compiler