Fundamentals of Roblox Scripting

Jul 5, 2024

Fundamentals of Roblox Scripting

Introduction

  • Purpose: Teach basics of Roblox scripting.
  • Audience: Beginners or those new to scripting.

Printing

  • Print function: Outputs text to the output window.
  • Example: print("hello world")
  • Data Types: Numbers, strings (text), Booleans (true/false), complex data types (instances, udim, Vector3 Values).

Properties

  • Definition: Attributes of objects (parts, GUI) that can change (size, color, position, transparency).
  • Accessing Properties: Use the properties window.
  • Hierarchy within the game:
    • Game: Parent of everything.
    • Services: E.g., workspace, lighting, which are children of the game.
    • Objects: Found within services.
  • Referencing: Locate an object within the game to change its property.
    • Example:
      game.workspace["part"].transparency = 0.75
      game.workspace["part"].anchored = false
      

Variables

  • Definition: Names that hold a value of any data type.
  • Declaration:
    • Syntax: local variableName = value
    • Example: local name = "scrips"
  • Usage of Variables: Replace repetitive code.

Functions

  • Definition: Blocks of code that execute when called.
  • Defining a Function:
    • Syntax:
      local function functionName()
      -- code block
      end
      
    • Example: Adding numbers:
      local function addNumbers(num1, num2)
        local result = num1 + num2
        print(result)
      end
      
  • Calling a Function: functionName()
  • Parameters: Placeholders within functions receiving values when called.
  • Returning Values: Use return to send values back to where the function was called.

Conditional Statements

  • If Statements: Execute code if specific conditions are true.
    • Syntax:
      if condition then
        -- code block
      end
      
  • Else Statements: Execute code if the if condition is false.
  • Elseif Statements: Additional conditions checked after the initial if fails.

Tables

  • Definition: Data type for storing multiple values of any type.
  • Arrays: Tables with ordered lists of values, indexed numerically.
    • Example:
      local arr = {"scrips", "is", "cool"}
      print(arr[1])  -- Outputs: scrips
      
  • Common Functions:
    • table.find: Finds an index of a value.
    • table.remove: Removes an element by index.
    • table.insert: Adds a new element to the table.
  • Dictionaries: Tables with key-value pairs instead of numerical indexes.
    • Example:
      local dict = {name = "scrips", favoriteColor = "green"}
      print(dict.name)  -- Outputs: scrips
      

Built-in Functions

  • Roblox-Specific Functions: Pre-written functions provided by Roblox Studio.
  • Examples:
    • task.wait: Pauses script execution for a specified time.
    • findFirstChild: Finds the first child object with a specified name.
    • getChildren: Returns a table of all children objects within an instance.

Loops

  • While Loops: Repeats code block while a condition is true.
    • Syntax:
      while true do
        -- code block
        task.wait(1)
      end
      
  • Repeat-Until Loops: Repeats code until a condition is met.
    • Syntax:
      repeat
        -- code block
      until condition
      
  • For Loops: Counts through numbers or iterates through tables.
    • Syntax:
      for i = 0, 10, 1 do
        -- code block
      end
      
    • Iterating Through Tables:
      for index, value in ipairs(myTable) do
        print(index, value)
      end
      

Events

  • Definition: Listeners waiting for specific actions (e.g., touching a part).
  • Connecting Functions to Events:
    • Example:
      local part = game.workspace.Part
      part.Touched:Connect(function()
        part.Transparency = 0.5
      end)
      
    • Player Added Event:
      game.Players.PlayerAdded:Connect(function(player)
        print(player.Name)
      end)
      

Conclusion

  • Recap of key topics: Printing, properties, variables, functions, if statements, tables, built-in functions, loops, events.
  • Encourage viewers to comment with tutorial requests.
  • Thank viewers for watching.