5.7.3 The Death Block

Although you may choose to implement your own, most Roblox player characters and NPCs are based around the Humanoid.

This is an object that gives models humanoid-like functionality such as movement, walking, climbing stairs, and the ability to wear accessories or hold items in their hand.

The Humanoid “brings the model to life” versus something like a vehicle model which cannot use most of the humanoid functionality.

It also contains a character’s health.

Meaning that if you had murderous intent, this will be the first place you would look. Say perhaps, I don’t know, maybe they touched your block and that was not okay.

You’d find their humanoid and make it so they don’t ever step all over your block again.

Lua
local part = game.Workspace.Part

part.Touched:Connect(function(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid") --Searches for object by class

	if humanoid then
		humanoid.Health = 0
		--humanoid:TakeDamage(1000000000000000000000)
		print(character, "has died ☠")
	end
end)

The humanoid health may be set directly to some value or by calling the TakeDamage method.

In an experience such as an obby where there might be a significant number of death blocks, connecting listeners using a loop is indispensable.

Lua
local folder = Instance.new("Folder")
folder.Parent = game.Workspace
local boardDimensions = 34
local blockSize = 6

local function createPart(position, color)
	local part = Instance.new("Part")
	part.BrickColor = color or BrickColor.new("Institutional white")
	part.Position = position
	part.Size = Vector3.new(blockSize, 0.5, blockSize)
	part.Anchored = true
	part.BottomSurface = Enum.SurfaceType.Smooth
	part.TopSurface = Enum.SurfaceType.Smooth
	part.Parent = folder
	return part
end

local function handleTouch(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")

	if humanoid and humanoid.Health > 0 then
		local explosion = Instance.new("Explosion")
		explosion.Position = character.PrimaryPart.Position
		explosion.Parent = character
		print(character, "has died ☠")
	end
end

game.Workspace.SpawnLocation.Duration = 0
local deathBlock = true

for xStep = -(boardDimensions/2), -1 do
	for zStep = -(boardDimensions/2), -1 do
		local x = (xStep * blockSize) + (blockSize / 2)
		local z = (zStep * blockSize) + (blockSize / 2)

		local dx = 1
		local dz = 1

		deathBlock = not deathBlock

		for _ = 1, 2 do
			deathBlock = not deathBlock
			for __ = 1, 2 do
				deathBlock = not deathBlock

				local pos = Vector3.new(dx * x, 0.5, dz * z)
				local part = createPart(pos, deathBlock and BrickColor.new("Really red") or nil)

				if deathBlock then
					part.Touched:Connect(handleTouch)
				end

				dz = -dz
			end
			dx = -dx
		end
	end
end