# The Basics Very basic things for beginners looking to learn how to start their luau scripting journey ## Table of Contents - [Variables](https://hackmd.io/@Drastic/r1RlbQTiA#Variables) - [About Variables](https://hackmd.io/@Drastic/r1RlbQTiA#About-Variables) - [What is a Variable](https://hackmd.io/@Drastic/r1RlbQTiA#What-is-a-Variable?) - [Use Cases](https://hackmd.io/@Drastic/r1RlbQTiA#What-are-some-use-cases-for-this?) - [How to use Variables](https://hackmd.io/@Drastic/r1RlbQTiA#How-to-use-Variables) - [Creating Variables](https://hackmd.io/@Drastic/r1RlbQTiA#Creating-Variables) - [Printing](https://hackmd.io/@Drastic/r1RlbQTiA#Printing) - [Instances](https://hackmd.io/@Drastic/r1RlbQTiA#Instances) - [Path Constructors](https://hackmd.io/@Drastic/r1RlbQTiA#Path-Constructors) - [Creating Instances](https://hackmd.io/@Drastic/r1RlbQTiA#Creating-Instances) - [Properties](https://hackmd.io/@Drastic/r1RlbQTiA#Properties) - [Methods](https://hackmd.io/@Drastic/r1RlbQTiA#Methods) - [If Statements and Loops](https://hackmd.io/fDxNipEaQ6CjWjvJtN-8Ig#If-Statements-and-Loops) - [If statements](https://hackmd.io/@Drastic/r1RlbQTiA#If-statements) - [Loops](https://hackmd.io/@Drastic/r1RlbQTiA#Loops) - [While Loop](https://hackmd.io/@Drastic/r1RlbQTiA#While-loops) - [Repeat until Loop](https://hackmd.io/@Drastic/r1RlbQTiA#Repeat-Until-Loops) - [Functions](https://hackmd.io/@Drastic/r1RlbQTiA#Functions) - [Creating Functions](https://hackmd.io/@Drastic/r1RlbQTiA#Creating-Functions) - [Running Functions](https://hackmd.io/@Drastic/r1RlbQTiA#Running-Functions) - [Function Parameters](https://hackmd.io/@Drastic/r1RlbQTiA#Function-Parameters) # Variables ## About Variables ### What is a Variable? A variable is an object in your code designed to be used as storage to reference to other values and objects efficiently. ### What are some use cases for this? A use case would be for setting a number in your script, for example if you want to store the number "10" you can make a variable for it and anything that needs that number can just use that variable instead, this is useful because if you for some reason need to change that 10 to an 11 you only need to change the 10 once instead of every single bit of code that will use that 10. <details> <summary>Correct and Incorrect Examples</summary> please note i dont expect you to understand any of the code in here, its just an example **Correct** ```lua -- Code Purpose: -- Add 1 to CurrentNumber variable until CurrentNumber is not less than ComparedNumber local CurrentNumber = 0 local ComparedNumber = 10 -- code below can use this variable, code above variable cant use it while ComparedNumber < CurrentNumber do CurrentNumber += 1 -- this is the same as doing CurrentNumber = CurrentNumber + 1 print(CurrentNumber, ComparedNumber) task.wait(0.5) end ``` **Incorrect** ```lua -- this code will not use any variables for the logic which will -- make it a lot harder to read and make it look messy -- Code Purpose: -- Loop through table and pring all the values until Index is -- Greater than 10 local TestTable = { 1,2,3,4,5,6,7,8,9,10,11,12,13 } for Index, Value in TestTable do print(Index) if Index > 10 then return end -- exits the loop if index is above 10 end ``` </details> ## How to use variables ### Creating Variables You can create a variable by typing `local` in all lowercase with whatever you want the variable to be named coming after it. To set a value to the variable you can put = and then whatever you want the variables value to be, you can set it to a string, number, boolean(true/false) or even another variable **Example:** ```lua local BlankVariable -- this is just the same as nil local NumberVariable = 10 local StringVariable = "Hello" local BoolVariable = true local VariableReference = NumberVariable ``` # Printing Printing lets you have a value appear into the console, you can do it using the `print()` function. ```lua local NewString = "hi" local NewNumber = 502 local NewTable = {1,2,3,4,5,6,7} -- Print each variable print(NewString) print(NewNumber) print(NewTable) -- however this is annoying to do. we can print all of these at once by separating them with commas. print(NewString, NewNumber, NewTable) -- Much easier ``` # Instances ## What are Instances? An instance is an object inside the game that will show up inside the roblox studio explorer. they have specific purposes and can be created by scripts. ## Path Constructors Instances can be accessed through something called a path, you can create a path using a dot notation (.) for all the instances until you find the instance you need ```lua -- Find a part in the workspace -- workspace can be accessed by just typing workspace workspace.Part game.Workspace.Part -- game.Workspace is unnecesary but will still work ``` To use a variable to find an instance you can use `:FindFirstChild()` (see methods section) or wrap a variable in brackets ```lua local InstanceName = "Part" workspace[InstanceName] workspace:FindFirstChild(InstanceName) ``` ## Creating Instances Instances can be created with the function `Instance.new()` and providing a string parameter, this will create a new instance with its own properties and methods. If you do not set the parent of a created instance then it will be parented to nil. ```lua local Part = Instance.new("Part") ``` ### Properties Properties are special values on an instance that tell instance how to act, for example changing the position on a part will make the part move and setting the parent will change the path of the instance. ```lua local Part = Instance.new("Part") Part.Parent = workspace Part.Anchored = true ``` You can also use a variable to access a child or property of an instance like this ```lua local ChildName = "Part" local Part = workspace[ChildName] local Part = workspace:FindFirstChild(ChildName) -- should use this instead of line above local PropertyName = "Color" local PartColor = Part[PropertyName] ``` ### Methods A method is a function that will directly run an action from an instance ```lua local Part = Instance.new("Part") local Part2 = Instance.new("Part") Part.Parent = workspace Part2.Parent = Part local MethodTest = Part:FindFirstChild("Part") -- Returns Part2 local MethodTest2 = Part2:FindFirstChild("Part") -- Returns nil local Children = Part:GetChildren() -- Returns a table with Part2 in it task.wait(3) Part:Destroy() -- Destroys instance and all its descendants ``` # If Statements and Loops ## If Statements **What is an "if statement"?** an if statement lets you check if a certain condition is met to run code. **Examples** ```lua if true then -- will run end if false then -- will not run, behind the scenes think about an if statement like -- a condition for if something is true, so it will only run if given -- a trueish value end if false == false then -- will run end if "hello" then -- will run because "hello" is not nil, nil is a falseish value -- in luau so if you put something in an if statement thats nil -- then it wont run the code end ``` ## Loops **What is a "Loop"?** a loop is a segment of code that will run multiple times. There are 3 kinds of loops! for loops, while loops, and repeat until loops, i will cover for loops later. ## Repeat Until Loop this will run a segment of code until a condition is not met, the condition is put after the "until" keyword, it acts the same as an if statement ```lua local Number = 0 repeat Number += 1 -- adds 1 to Number print(Number) until Number >= 5 -- Stops the loop when Number is greater than or equal to 5. ``` ## While Loops this is basically the same thing as a repeat until loop however one big difference is that unlike the repeat until loop this checks the condition BEFORE running the code inside the loop, whereas repeat until will check AFTER, so keep that in mind. ```lua local number = 0 while number < 6 do number += 1 print(number) end ``` # Functions Functions are basically a chunk of code that you can have your scripts choose to run at any time, they can be given values that let the code inside of them behave differently. ## Creating Functions To create a function you type `local function FunctionNameHere()` and then an `end` block ```lua local function ThisIsAFunction() -- Code Goes here end ``` After doing this you can write any code you want to run inside of this function you made. You can also create a global function inside a script. this lets any code inside that script use the function even if its above where the function itself is. ```lua -- local function, code above it cant access it local function LocalFunction() end -- Global Function function GlobalFunction() end ``` ## Running Functions To run a function you have created you can type the functions name followed by parenthasis (i cant spell) ```lua local function TestFunction() print("Function Ran") end TestFunction() ``` ## Function Parameters Function parameters are a value you can give to a function when calling it. to define the parameters a function will recieve you put the parameter names inside of the () when creating the function. To provide these parameters to the function you just have values separated by commas in the order that the function will expect them. ```lua local function ParameterTesting(Parameter1, Parameter2) print(Parameter1, Parameter2) end ParameterTesting("String", 123) -- Output: String 123 ParameterTesting(true) -- Output: true nil ``` If a parameter is not provided then it will be nil when the functions code runs.