Coin Factory
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 8 of 24.
A table holds a list of values in curly braces, and a for loop can visit each one:
local heights = { 4, 8, 12 }
for _, h in heights do
print(h)
endThis prints 4, 8 and 12. Each round, h holds the next value. The _ is the position in the list, which this loop does not need.
Combine a table with a function and you get a factory: one call per value.
local function makeTree(x)
-- build a tree at x
end
for _, x in { -10, 0, 10 } do
makeTree(x)
endCanCollide = false lets players walk through a part; it is still there and still visible.
A Folder groups parts, like workspace.Coins. Setting a part's Parent to a folder puts it in the world inside that folder.
Challenge
EasyFill in makeCoin(z) so it makes one coin, then call it for every number in spots.
Each coin is a Part that:
- is named
Coin - is a ball (
Enum.PartType.Ball), size2, 2, 2, and gold:Color3.fromRGB(255, 200, 40) - lets players walk through it (
CanCollideisfalse) and does not fall - is at Position
0, 3, z - goes in the
workspace.Coinsfolder
The coins do nothing yet. You will make them collectable in a later lesson.
Try it yourself
local spots = { 12, 6, 0, -6, -12 }
local function makeCoin(z)
-- Make a gold ball named Coin at (0, 3, z)
-- and put it in workspace.Coins
end
-- Call makeCoin for every spot
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Roblox Game Scripting
2Loops and Functions
Row of PillarsStaircase BuilderTower BuilderCoin FactoryBlinking LightVanishing Bridge5Your First Game
Your First ObbyPractice on your own: Online Lua compiler