31. From Lua to Luau — Homework solutions

These problems do not run in a terminal — they are Roblox snippets. To try them, paste each into a Script in Roblox Studio and press Play.

Problem 1 — Spot the difference

Sample answers (at least three of):

  • Lua uses io.read for input; Luau reads from in-world events.
  • Lua calls greet directly; Luau connects it to an event (Players.PlayerAdded:Connect(...)) and lets the engine call it.
  • 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 no game and 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 = workspace

Order matters: set every property before Parent. A Part appears the moment it is parented to workspace, so setting properties first avoids a flash at the wrong size or colour.

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 in workspace, 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, or otherPart.
  • In Roblox, print writes to the Output panel inside Studio, not a terminal.

Common mistakes.

  • Writing part.Touched(function ...) without :Connect. That is an error — Touched is an event, not a function you call.
  • Using Touched:Wait() instead of :Connect. :Wait() catches only one touch, then stops. For an event that should keep firing, use :Connect.

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)
end

task.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) (no task. prefix). It still works but is the legacy form — less accurate and on the way out. Use task.wait.
  • Forgetting task.wait, so the infinite loop locks Studio. Always put a wait inside a while true loop.

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.