Menu
CoddyTech

Row of Pillars

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

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

Turn the one pillar into a row of 5.

  1. Wrap the pillar code in for i = 1, 5 do ... end.
  2. Change the Position to Vector3.new(i * 6, 4, 0).

Press Play: five pillars stand in a row, at X 6, 12, 18, 24 and 30. 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

All lessons in Roblox Game Scripting

Practice on your own: Online Lua compiler