Sanity
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    2
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Intro to Luau: A Beginner's Guide ###### Tags: Roblox, Luau, Roblox Scripting, Scripting, Programming, Roblox Code - Table of contents: [ToC] In this HackMD, you will be learning what Luau is, the basics of Luau (Roblox-Lua, RBX.lua), and some reliable sources on learning Luau. ## What is Luau? Luau (pronounced Loo-uh U) is a fast, small, safe, gradually typed embeddable scripting language derived from Lua. It is used by Roblox game developers to write code, as well as by the Roblox Engineers to write core mechanics for the editor (Roblox Studio). Luau uses Lua 5.1. To check which version of Lua Luau is using, you could run: ```lua= print(_VERSION) --> Output: Lua 5.1 ``` in the command bar, or just go to https://luau-lang.org/ (The official Luau website) for more information. ## Basics of Luau In this section you will learn the Basics needed to create a sucessful script. ### Comments Luau code will ignore certain lines if prefixed with certain characters. In comments, you can write anything and it doesn't need to follow the code grammar / rules. Single line comments are prefixed with ``--`` while Multiline comments are prefixed with ``--[[`` and end with ``]]``. Example: ```lua= -- This is a comment! print('Hi!') -- Comments are ignored while code still works. --[[ This is a Multiline Comment! ]] ``` You can also use multiline comments for a single line. This is useful for having the code ignore certain parts of the line without ignoring the whole line. Example: ```lua= print(1 + 1 --[[* 2]]) -- Output: 2 ``` In the code above, the output is 2. Why? Well because the '* 2' portion is ignored, so it wouldn't multiply by 2 and only add 1 by 1. ### Services A service, in scripting, is an object that contains built-in properties, functions, events, and callbacks. These services are used to reach a goal. For example, MarketplaceService is used to handle in-game purchases, while HttpService is used for getting information from external websites. A list of Roblox Services can be found [here](https://roblox.fandom.com/wiki/Category:Services). To get a service with a script on Roblox, you would need to use: ``ServiceProvider:GetService()`` Example: ```lua= local Marketplace = game:GetService('MarketplaceService') ``` Using that code as an example, you could then use the functions, events, properties, etc, of the service. For more information on services and their uses, go to the [Roblox API](https://developer.roblox.com/en-us/api-reference). ### Scripts Scripts are containers for Luau code. The default content for scripts when you create a new Local/Server Script is: ```lua= print('Hello, world!') ``` The default content for a Module Script is: ```lua= local module = {} return module ``` There are 3 types of scripts: - Local Scripts (LocalScript) - Server Scripts (Script) - Module Scripts (ModuleScript) Local Scripts are used for executing Luau code on the client. Server Scripts are used for executing code on the server. Modules Scripts are a bit different, though. Module Scripts **do not** execute by themselves when the game has started running. Module Scripts need to be required by either a Server Script or a Local Script in order to be run. Local Scripts **do not** run when parented by a service owned by the server. (ServerScriptService, ServerStorage, Workspace, etc) Server Scripts **do not** run when parented by a service owned by the client. (ReplicatedFirst, StarterPack, StarterGui, etc) ReplicatedStorage is a neutral service, allowing both Server and Client to run. ServerScriptService and ServerStorage **is** invisible to the client, and ReplicatedFirst **is** invisible to the server. - More information on Local Scripts can be found [here](https://developer.roblox.com/en-us/api-reference/class/LocalScript). - More information on Server Scripts can be found [here](https://developer.roblox.com/en-us/api-reference/class/Script). - More information on Module Scripts can be found [here](https://developer.roblox.com/en-us/api-reference/class/ModuleScript). ### Variables A variable is a name that holds a value. Variables can be either be a [Number](https://developer.roblox.com/en-us/articles/Numbers), [String](https://developer.roblox.com/en-us/articles/String), [Boolean](https://developer.roblox.com/en-us/articles/Boolean), [Data Types](https://developer.roblox.com/en-us/api-reference/data-types), etc. There are two scopes for variables: Local, and Global. #### Defining Variables There are 3 types of variables in Luau. Example: ```lua= local x = 1 y = 1 _G.z = 1 ``` - _G. Variables **do not** follow Scopes and can be used across scripts. - Global Variables **do not** follow Scopes and can only be used in the script it is defined in. - Local Variables **do** follow scopes and can only be used in the script it is defined in. You can also define multiple variables in a single line. Example: ```lua= local x, y, z = 1, 2, 3 ``` OR ```lua= a, b, c = 1, 2, 3 ``` ### Scopes ###### Figure 1 (Credits: Roblox Developer API) ![](https://developer.roblox.com/assets/bltd46ee264544ef2f3/Scope-Diagram.png) - Block B **can** access the local variable in block A. - Block C **can** access the local functions/variables in blocks A and B. - Block A **can not** access the local functions/variables in blocks B and C. - Block B **can not** access the local functions/variables in block C. Example: ```lua= local function foo() local name = 'Bob' print(name) --> Output: Bob end print(name) --> Output: nil ``` More information on Scopes can be found [here](https://developer.roblox.com/en-us/articles/Scope). ### Functions Functions are sets of instructions that can be used multiple times in a script. A function can be executed through a command or triggered through an event. #### Defining Functions A basic function declaration uses the ``function`` keyword, following with the name of the function and ending with two parenthesis ``()``. Example: ```lua= local function foo() --> function name: 'foo' -- Function body end ``` When called, functions will execute the code in the function's body. To call a function, first make sure your function is in the scope of your script, then type the function's name and end with two parenthesis: ``foo()`` Example: ```lua= local function foo() print('This function has been called!') end foo() --> Output: This function has been called! ``` #### Function Arguments & Parameters A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is being sent to the function whenever the function is called and executed. Example: ```lua= local function foo(text) print(text) end foo('Hi, my name is Bob!') --> Output: Hi, my name is Bob! ``` #### Returning Values Return is widely used for returning a value. Example: ```lua= local function add(number1, number2) return number1 + number2 end local newNumber = add(1, 3) print(newNumber) --> Output: 4 ``` ### Events Events send out signals when specific things happen in a game. For example, if a player touches a part, it would fire the ``Part.Touched`` event. Example: ```lua= local Part = game.Workspace.Part Part.Touched:Connect(function(hitPart) Part.Color3 = Color3.new(255, 0, 0) end) ``` In the example above, the ``.Touched`` function will only fire whenever another Part touches the part. More information on Events can be found [here](https://developer.roblox.com/en-us/articles/events). ### Conditional Structures Conditional structures allow scripts to perform actions when specific conditions are true. Conditions can be checked with the **relational operators** list summarized here: | Operator | Description | Example | | -------- | ----------- | --------------- | | == | Equals to | 1 == 2 🠒 False | | ~= | Not Equals to | 1 ~= 2 🠒 True | | > | Greater Than | 1 > 2 🠒 False | | < | Less Than | 1 < 2 🠒 True | | >= | Greater Than or Equals to | 1 >= 2 🠒 False | | <= | Less Than or Equals to | 1 <= 2 🠒 True | #### If Statement Booleans, Strings, Data Types, and Numbers can all use the **relational operators**. Booleans and Strings can only use the ``Equals To`` and ``Not Equals To`` operators. Example: ```lua= local Number = 10 if Number == 10 then print('The number is ten!') --> Output: The number is 10! end ``` You can also do the same for Booleans. Example: ```lua= local Boolean = true if Boolean == true then print('True!') --> Output: True! end ``` You can do the same with Strings. Example: ```lua= local String = 'Hi!' if String == 'Hi!' then print('Hi!') --> Output: Hi! end ``` ...And finally, you can do the same with Functions. Example: ```lua= local function foo() return true end if foo() == true then print('True!') --> Output: True! end ``` #### Else Statement There are also ``else`` statements in Luau. ``else`` is only executed when the conditions are not met. You **cannot** start a conditional structure with else. Example: ```lua= local Boolean = false if Boolean == true then print('True!') else print('False!') end --> Output: False! ``` In the example above, it will print 'False!' because the Boolean variable is false and the if statement will only run if the Boolean variable is true. #### Elseif Statements ``elseif`` is basically another ``if`` statement. You **cannot** start a conditional structure with ``elseif``. Example: ```lua= local Number = 10 if Number == 1 then print('One!') elseif Number == 10 then print('Ten!') else print('It is not ten nor one. :(') end --> Output: Ten! ``` ## End Thank you for reading ``Intro to Luau: A Beginner's Guide``! I hope this HackMD has helped you, and I wish you good luck to your scripting career! :D Thanks, Luke (DemolishSanity). ## Sources / Reliable Websites - [Roblox API](https://developer.roblox.com/en-us/api-reference) - [Stack Overflow](https://stackoverflow.com/) - [Roblox Fandom](https://roblox.fandom.com/wiki/) - [Official Luau Website](https://luau-lang.org/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully