Menu
CoddyTech

Kill Bricks

Lesson 4 of 18 in Coddy's Roblox Game: Endless Tower course.

Every obby has red parts you must not touch. In the stages they are marked with a tag, a label you can put on any instance. The red strips carry the tag Kill:

print(part:HasTag("Kill")) --> true

Tags are how big games stay simple: the stage builder only tags a part, and one function makes every tagged part work. Your tower's stages are copies, so set them up after they are built. GetDescendants lists every part inside a stage, however deep:

for _, part in stage:GetDescendants() do
	if part:HasTag("Kill") then
		part.Touched:Connect(killOnTouch)
	end
end

To end a life, set the Humanoid's Health to 0. A touch can come from anything, so check the toucher's model has a Humanoid first. In this lesson every stage is KillBricks, so you can try it.

challenge icon

Challenge

Easy
  1. Write killOnTouch(hit): if the toucher's model has a Humanoid, set its Health to 0.
  2. Write setupStage(stage): for every part in stage:GetDescendants() tagged Kill, connect its Touched to killOnTouch.
  3. In buildTower, call setupStage(stage) for each stage you stack.

Press Play and jump over the red strips. Then Check.

Try it yourself

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local stages = ServerStorage.Stages
local tower = nil

print("Each tower has " .. Settings.StageCount .. " stages")

-- Every player gets Wins on the leaderboard
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
end)

-- Any stage, at random
local function pickStage()
	local all = stages:GetChildren()
	return all[math.random(1, #all)]
end

-- Put a copy of a model on top of the tower; returns the copy and the new top
local function stack(template, bottom)
	local copy = template:Clone()
	local box, size = copy:GetBoundingBox()
	local lift = bottom - (box.Position.Y - size.Y / 2)
	copy:PivotTo(copy:GetPivot() + Vector3.new(0, lift, 0))
	copy.Parent = tower
	return copy, bottom + size.Y
end

-- Build a new random tower, bottom up
local function buildTower()
	tower = Instance.new("Model")
	tower.Name = "Tower"
	tower.Parent = workspace
	local top = 0
	for i = 1, Settings.StageCount do
		local stage, newTop = stack(pickStage(), top)
		top = newTop
	end
	stack(ServerStorage.Finish, top)
end

buildTower()
quiz iconTest yourself

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

All lessons in Roblox Game: Endless Tower