Menu
CoddyTech

Row of Pillars

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

To make five pillars you could copy the same lines five times. A for loop runs them for you, as many times as you ask:

for i = 1, 3 do
	print(i)
end

This prints 1, then 2, then 3. Everything between do and end runs once per round, and i holds the round number.

Because i changes every round, you can use it in a Position and each copy lands somewhere new:

for i = 1, 3 do
	local ball = Instance.new("Part")
	ball.Shape = Enum.PartType.Ball
	ball.Position = Vector3.new(i * 10, 20, 0)
	ball.Parent = workspace
end

Three balls drop from X 10, 20 and 30: i * 10 is 10, then 20, then 30.

challenge icon

Challenge

Easy

The script makes one pillar at X 6. Turn it into a row of 5:

  1. Wrap the pillar code in for i = 1, 5 do ... end.
  2. Print i at the start of each round.
  3. Change the Position to Vector3.new(i * 6, 4, 0), so the pillars stand at X 6, 12, 18, 24 and 30.

Press Play and walk between them, then Check.

Try it yourself

-- This makes ONE pillar. Make 5, in a row.
local pillar = Instance.new("Part")
pillar.Name = "Pillar"
pillar.Size = Vector3.new(2, 8, 2)
pillar.Position = Vector3.new(6, 4, 0)
pillar.Anchored = true
pillar.Parent = workspace
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