Menu
CoddyTech

Lava Floor

Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 12 of 24.

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

The glowing orange strip is lava, and this script is inside it. When something touches the lava, find a Humanoid in hit.Parent and, if there is one, set its Health to 0.

Make sure your script survives things that are not players: one check drops a ball on the lava.

Press Play and walk into the lava (you respawn after 5 seconds), then Check.

Try it yourself

local lava = script.Parent

lava.Touched:Connect(function(hit)

end)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Roblox Game Scripting

Practice on your own: Online Lua compiler