Menu
CoddyTech

Tower Builder

Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 7 of 25.

A function is a named piece of code you can run whenever you need it. Values you pass in are called parameters:

local function greet(name)
	print("Welcome, " .. name)
end

greet("Ava")
greet("Leo")

.. joins two strings, so "Welcome, " .. "Ava" is Welcome, Ava. The code prints Welcome, Ava and Welcome, Leo. Defining the function only stores it; each call, like greet("Ava"), runs its code with name set to what you passed.

A function can take several parameters, separated by commas, and use them anywhere inside, like in a Size:

local function makeWall(width, height)
	local wall = Instance.new("Part")
	wall.Size = Vector3.new(width, height, 1)
	wall.Anchored = true
	wall.Parent = workspace
end

makeWall(10, 6)

makeWall(10, 6) sets width to 10 and height to 6, in that order.

A part grows equally up and down from its Position, so a part that is h tall stands on the ground (Y 0) when its center is at h / 2.

challenge icon

Challenge

Easy

Finish makeTower(x, height) and use it three times.

  1. Set the tower's Size to Vector3.new(4, height, 4).
  2. Set its Position to Vector3.new(x, height / 2, 0), so it stands on the ground.
  3. Call it for (-10, 6), (0, 12) and (10, 18).

Press Play: three towers stand in a row, each taller than the last. Then Check.

Try it yourself

local function makeTower(x, height)
	local tower = Instance.new("Part")
	tower.Name = "Tower"
	tower.Anchored = true
	-- Size: 4 wide, height tall, 4 deep
	-- Position: at x, standing on the ground (center at height / 2)
	tower.Parent = workspace
end

makeTower(-10, 6)

All lessons in Roblox Game Scripting

Practice on your own: Online Lua compiler