31. From Lua to Luau — Homework solutions
These problems do not run in a terminal — they are Roblox snippets.
Problem 1 is a written answer. To try the others, paste one into a
Script in Roblox Studio and press Play.
Problem 3 also needs a Part named Coin in Workspace and the
line local part = workspace.Coin above the snippet. The
challenge needs a Part named BlinkPart in Workspace.
Problem 1 — Spot the difference
Sample answers (at least three of):
- Lua uses
io.readfor input; Luau reads from in-world events. - Lua calls
greetdirectly; Luau connects a function to an event (Players.PlayerAdded:Connect(...)), the engine calls that function, and it callsgreet. - Lua's
print(...)writes to the terminal; in Roblox it writes to the Output panel inside Studio. - Luau reads
player.Name— a property of an Instance. Plain Lua has no Instances at all. - Luau uses
game:GetService("Players")to find the Players service. Plain Lua has nogameand no services.
Any three (or similar) count. The logic is the same; only the plumbing around it changes.
Problem 2 — Make a Part appear
Worked solution.
local part = Instance.new("Part")
part.Size = Vector3.new(4, 1, 4)
part.Position = Vector3.new(0, 10, 0)
part.BrickColor = BrickColor.new("Bright red")
part.Anchored = true
part.Parent = workspaceSet every property before Parent. A Part goes
live the moment it is parented to workspace, and each
change after that has to be sent to every player, so setting properties
first saves that work.
Common mistakes.
- Forgetting
part.Anchored = true. Without it, gravity pulls the Part down. Coins and decorations almost always want anchoring. - Forgetting
Parent. The Part exists but is not inworkspace, so nobody sees it.
Problem 3 — Wire up Touched
Worked solution.
part.Touched:Connect(function(other)
print(other.Name .. " touched the part")
end)Three things to notice:
:Connect(...)registers the function; it does not run it. The function runs later, when the event fires.- The function receives the toucher as its one argument — often named
other,hit, orotherPart. - In Roblox,
printwrites to the Output panel inside Studio, not a terminal.
Common mistakes.
- Writing
part.Touched(function ...)without:Connect. That is an error —Touchedis an event, not a function you call.
Challenge — Blinking part
Worked solution.
local part = workspace.BlinkPart -- assume a Part named BlinkPart
local red = BrickColor.new("Bright red")
local blue = BrickColor.new("Bright blue")
while true do
part.BrickColor = red
print("red")
task.wait(1)
part.BrickColor = blue
print("blue")
task.wait(1)
endtask.wait(1) pauses the script for one second. While
paused, Roblox keeps doing everything else — physics, other scripts,
player input. When the second is up, the script resumes from the line
after task.wait.
The loop is while true because it should blink forever.
Roblox stops the script when the game stops, so you need not arrange a
clean exit.
Common mistakes.
- Using the older
wait(1)(notask.prefix). It still works but is the legacy form — less accurate and on the way out. Usetask.wait. - Forgetting
task.wait, so the infinite loop locks Studio. Always put a wait inside awhile trueloop.
Done?
The next four chapters go deeper into Roblox — Instances and the Explorer, Events and connections, Services and the data model, and leaderstats and value objects — and then Part 7's two mini-projects, the Touch-to-Collect Coin and Collect-All-Coins, put it all into real Studio scenes.