Instead of watching and waiting, let Roblox call your code when something happens — a touch, a click, a player joining.
So far your scripts run top to bottom and finish. But games aren't linear — a player can touch a coin any time, or never. How do you write code that waits patiently and springs into action only when the moment comes? You don't wait. You listen. That's events.
An event is a signal an object fires when something happens. When you write:
part.Touched:Connect(function(hit)
print("Something touched the part!")
end)
You're not running anything yet. You're telling Roblox: "whenever part gets touched, call this function." The function is a callback — code that isn't run by you, but by the engine, at exactly the right moment.
This is the single most useful pattern in Roblox. Most of your game will be event callbacks.
What does :Connect do?
The .Touched event fires whenever any other part touches the part. It passes one argument to your function: the thing that touched it.
local coin = workspace.Coin
coin.Touched:Connect(function(hit)
print("Touched by " .. hit.Name)
end)
Note the function is anonymous — no name, created in place, and passed straight to :Connect. The engine will call it with hit filled in whenever the event fires.
Because physics are loose, .Touched can fire with odd objects or repeatedly as parts jostle. In a later lesson you'll check whether the touching thing is actually a player's character.
A ClickDetector is a child object that makes a part clickable. The .MouseClick event fires when a player clicks it.
local button = workspace.MysteryBox
button.ClickDetector.MouseClick:Connect(function(player)
print(player.Name .. " clicked the box!")
end)
MouseClick gives you the player who clicked — exactly who you need for player-specific rewards.
The MouseClick callback function receives which value?
Events are everywhere in Roblox:
| Event | Fires when |
|---|---|
part.Touched | something touches a part |
part.ClickDetector.MouseClick | a player clicks a clickable part |
game.Players.PlayerAdded | a player joins the server |
player.CharacterAdded | a player's character spawns |
They're all connected the same way: thing.EventName:Connect(function(...) end).
Make a part named coin respond when clicked. Write the connect line so it prints the clicking player's name.
local coin = workspace.Coin
-- your connect here
:Connect() when you meant to call the function. coin.Touched:Connect(myFunction()) calls myFunction immediately and connects its return value — usually nil, so nothing ever runs. Pass the function without parentheses: :Connect(myFunction).while true stacks a new callback every tick, and the event fires all of them at once. Connect once, at the top of your script.That's the whole Luau basics box: scripts, variables, types, numbers, strings, booleans, functions, conditionals, loops, tables, timing, and events. Next section: data — how a player's progress is stored and trusted.