PcoWSkbVqDnWTu_dm2ix
Deadly Lava
Part 2 - Killing the Player
Deadly Lava
Part 2 - Killing the Player

Character and Humanoid

When a player touches the lava, Roblox actually detects the specific body part of the player that touched it, such as the left leg or right foot. This part is in the player’s Character model.

Character Model

The Character model contains all of the objects that make up the player’s avatar in the game, including:

  • The individual body parts of the player such as the head, limbs, and torso.
  • Any clothing and accessories worn by the player.
  • The Humanoid, a special object which contains many properties related to the player, including the player’s health.
  • The HumanoidRootPart which controls the player’s movement.

As noted above, any body part that touches the lava is part of the Character model, so you can get a reference to that character with otherPart.Parent.

  • Create a variable to store the parent of the part that touched the lava floor.
local lava = script.Parent

local function killPlayer(otherPart)
	local partParent = otherPart.Parent
end

lava.Touched:Connect(killPlayer)

From the character model, you’ll need to get the Humanoid object in order to kill the player. This can be done with the FindFirstChild function - just pass it the name of the thing you’re looking for and it will provide the first matching child it finds in that object.

  • Call FindFirstChild on the partParent variable with "Humanoid" as the child to find, and store the result in a new variable called humanoid.
local lava = script.Parent

local function killPlayer(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChild("Humanoid")
end

lava.Touched:Connect(killPlayer)

Checking the Humanoid

You can easily check if the Humanoid was found using an if statement. The code in an if statement will only run if the condition defined in the first line is true.

There are a variety of operators that can be used to build more complex conditions which you’ll encounter in future courses - for now, just put the humanoid variable there.

  • Create an if statement with humanoid as the condition.
local lava = script.Parent

local function killPlayer(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChild("Humanoid")
    if humanoid then

	end
end

lava.Touched:Connect(killPlayer)
Boolean Logic

In Lua, any value other than false or nil (an empty value) is considered to be true. In other words, the following if statements behave the same, as == means “is equal to.”

if humanoid then

end
if humanoid == true then

end

Some developers prefer the shorter version while others prefer the more complete style - just note that both are valid Lua and behave identically.


Setting Character Health

Now that the Humanoid has been checked, its properties can be changed. If you set its Health property to 0, the associated Character will die.

  • In the body of the if statement, set the Health property of humanoid to 0.
local function killPlayer(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end

lava.Touched:Connect(killPlayer)

With that, your lava floor is complete! Test your game and you should find that your deadly lava successfully kills players on contact. Try using your lava as an extra challenge in an obby, or as a boundary for a game world.

local lava = script.Parent

local function killPlayer(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end

lava.Touched:Connect(killPlayer)


Previous Page Getting Started