io.read
, we mean "the read
entry from the io
package". For Lua, that means "index the table io
using the string "read"
as the key".{}
=
while the value is on the right side.x
starts at 0, increments by 1 in each iteration of the for
loop, and is printed again afterward with a final value of 5.local
statement:x
is set to 0, and another local variable x
is declared inside the for
loop. As the loop iterates, its locally-scoped x
is printed with a constant value of 1, then the initial variable x
is printed with an unchanged value of 0.function
keyword followed by the function name and a pair of parentheses (()
). Since the function’s body will be a block of code, it must be closed with the end
keyword.()
and end
is where commands and other code make up the function body. These commands will be executed when the function is called:()
):nil
will be used for all missing parameters.return
values. Working off the example above, the following function returns the sum instead of printing it:return
command as doing so will generate an error.delay()
call or a PlayerAdded
event connection:end
closure before the ending )
of a containing function or event like delay()
or :Connect()
.ModuleScripts
where the module’s table contains various functions:ModuleScript
via require()
:Wait()
function will cause the script to pause until the event occurs once. When it does, the function returns the data associated with the event’s firing.Connect()
function can be used when a given function should run every time an event fires. This function immediately returns a connection object. In the example below, we connect a function, onTouched()
, to a Part
in the Workspace
. If another part collides with myPart
, the script prints the name of the other part involved in the collision.Disconnect()
method of the connection object returned by Connect()
.Disconnect()
when a connection is no longer needed. Forgetting to do so can cause your game to use more resources than necessary. Note, however, that this may not always be necessary; when an object is destroyed, all connections to that object’s events are disconnected automatically.Player
joins the game, the Players.PlayerAdded
event fires with a reference to the new player.CharacterAdded
event of the Player
involved in the Players.PlayerAdded
event.Player
object is destroyed. This causes the connection to CharacterAdded
made inside onPlayerAdded()
to be disconnected, so you don't need to call Disconnect()
in this case. In other circumstances where the object in question is not necessarily destroyed, you may need to use a table to keep track of connections to be disconnected later via another function.BindableEvent
which features a single user-fireable event named Event
.BindableEvent
that already exists in the game hierarchy or create one using Instance.new()
. Then, call Fire
with any relevant data to trigger the Event
. For example:BindableEvent
, but more complicated data such as functions or tables with metatables will require a more custom solution. See the documentation for BindableEvent:Fire()
for details on the kinds of data that can be sent when firing bindable events.RemoteEvent
can be used. This works similarly to BindableEvent
but is network-ready.