This topic is intended for developers who is inexperienced in scripting in roblox studio and you need a basic knowledge on what is code, code is basically instructing a computer to do something and this topic will be simplified and short as possible.
I recommend and encourage you to read every single line in this topic carefully so you won’t get confused
The Fundamentals of Scripting
- Variables - Stores values in containers for references and reusability
- Loops - Repeats code in a loop
- Functions - Functions are reusable block of codes that can be executed when called
- Logic - When, what and how code runs in your script
- Operators - Describes all operators in scripting
- Events - Events are things that happen for specific things
- Client & Server - Client is the player itself using the device, Server is the main thing who owns and manages almost everything in the game
- Dot notation - a way to access properties and objects in the explorer hierarchy
Variables & Loops
Variables are values stored in containers reserved as references, There are two types of variables:
- Global Variables - Can be accessed or used anywhere in the script
- Local Variables - Can only be accessed in the area of the code it is in
Global Variables may be tolerated to be used but i strongly discourage you to not use it and instead use local variables which you can still access it and use it anywhere if you create it in top of the script
Declaring and assigning a value to a local variable means creating a variable:
-- local variables
local number = 1
local mystring = "string"
local on = true
local off = false
local means the variable is a local variable as said previously and after that you may see words like “number”, “mystring”, on and off there but those act like the name of the container who stores the value which acts as the reference so they can be any name as what you prefer
= 1, = "mystring", = true, = false these assign a value to the variable, assigning a value to a container basically means storing a value to the container
Declaring a variable means creating a variable but not yet assigning it:
local variable
here you can reserve variables for future values it will be assigned to, this is like creating a container but not storing any value in it
If you want to assign it a value you do this:
local variable
variable = 1
the code above is the full code of how you would declare and assign a variable, the variable = 1 can be in any line so you can assign it later or now
local number = 1
local mystring = "string"
local on = true
local off = false
As you can see in the code above it declares and assigns the variable now instead of reserving the variables, this way of declaring and assigning variables is mostly commonly used so stick to this way.
each variable has different types of values in the code above, the first variable called number is a reference to the value that is the number 1, so the first variable is a number
second variable is a string, strings are group of characters or text that can be either wrapped in double quotes "string" or single quotes `‘string’
the on & off variables are called booleans, booleans are commonly used in scripting and booleans are types that act like switches, for example if a switch that is a boolean was on, its value would be true otherwise if the switch that is a boolean was off its value would be false
if true and false values are colored orange in your script then their type is a boolean, properties in objects in roblox studio are different and theres this thing called checkboxes properties, those checkboxes properties are boolean
nil is a value or keyword to represent nothing, you can use the nil keyword as values for things such as variables
A summary of common types in variables and others things:
- string - a group of characters wrapped in double or single quotes
- number - a number is a number that can be any type of number
- boolean - the type of true and false values
Loops in roblox scripting allows code to be repeated indefinitely or certain amount of times by a loop
there are 3 types of loops:
- for loop - repeats code for specific amount of times
- while loop - repeats code indefinitely based on a boolean aka conditions, if the boolean’s value is changed or inverted the loop stops
- repeat until - repeats code indefinitely until the given condition is met
to use a for loop:
for i=1, 10 do
print(i)
end
essential keywords in the code:
- for - very important for creating loops otherwise it wont function
- do - very important for creating loops otherwise it wont function
- end - last essential keyword, it is where the repeated block of code lines will end at
Block of codes or codes inside for loops ends at the line before the end keyword, without the end keyword the rest of the code after the for loop will be repeated so you only wanna affect the code inside the for loop
i=1 this declares a variable called “i” and assigns it to the value 1, the 10 is how much code would be repeated, if “i” was 1 the code would repeat for the same number of times as the number after the variable was declared and assigned otherwise if the “i” is a different number like 2 or 5 or 3 you get the number of times the code would be repeated and subtract it by the “i” value and add 1 by the different, the result is how much your code would repeat
also the i=1 acts like a counter so it would increment up by 1 until it reaches the same number the code would be repeated and you can use the i as your counter instead of creating another variable that you will make it add by 1
READ THIS!!!
you have to add
task.wait()in any line of the block of the code inside all loops in roblox scripting to prevent the game from crashing, thetask.wait()ensures that the code will be repeated by the fastest possible rate without freezing or crashing your device,task.wait()basically yields the rest of the code after its line by the smallest delay
to use a while loop:
while condition do
end
the condition is a boolean value, while and do and end are essential for creating a loop and as i said previously the end keyword is where the block of code inside the loop would end so it wont have to repeat the rest of the code after it, this same goes for other loops and other things that include the essential keyword end
the while loop repeats code inside the loop indefinitely until the condition inverts or changes to a different boolean value so it would stop forever if the condition changes and wont run again
to use a repeat until loop:
repeat
until condition
the block of code that will be repeated will be the lines between the lines of repeat and until so the code inside the loop will be repeated
this loop basically repeats the code inside the loop until the condition is true
the condition word i used in every loop code i showed you is just a representation of how booleans would be used in loops so dont blame me if you run the code and it results into an error as you need to declare the variable condition first
Keywords in loops:
- break - stops the loop from repeating the code if you type it in inside the loop
- continue - skips the rest of the code from running after its line if you type it inside the loop
Functions, Arithmetic Operators and other keywords
arithmetic operators are the 4 basic operators in math to manipulate numbers, addition, subtraction, multiplication and division
Summary of the other keyword in roblox scripting:
- return - used in functions to return a value to the function or stop the function from running the rest of the code after the return keyword
Functions are reusable block of code that when a function is called it executes the block of codes inside of it
to make a function:
local function myfunction()
end
the end keyword is self explanatory as it is where the block of code ends, local means the function is a value in a variable that is a local variable, myfunction is the name of the variable and () is essential for creating functions
a code inside a function serves as the block of code inside the function
to execute the block of code inside the function you would call the function like this
myfunction()
you can put names and words inside the () like this
local function myfunction(a, b)
end
a, b is a tuple which consists of two values, think of tuple like a list of values, tuples are basically a list of values separated by a comma a, b, c, d, e, f, g
so those values a, b are called parameters which are placeholder values which the function will use as information or data
local function myfunction(a, b)
return a + b
end
as you can see the code above i am using the a and b parameters and treat them like numbers and then use the return keyword to return the result or the sum of the a + b
this means that if you call the function, fill in the a and b data like this
myfunction(1, 1)
that code above becomes the value of the result from adding the two numbers, a + b and i filled it in with numbers when i called the function and you can use the code above as a value directly
local function myfunction(a, b)
return a + b
end
local result = myfunction(1, 1)
print(result)
when you test play in roblox studio the print(result) prints the result to the output and so it would be the number 2 in the output
when you use print("Hello World") you are actually calling a function from the roblox engine and this function will print the string “Hello World” to the output so anything that has () after the name is a function and you are calling it
Operators | Comparison Operators
Comparison operators return a boolean value
Summary of all comparison operators:
==- an operator that checks if a value is equal to a value, if its equal to it returns true otherwise false>- an operator that checks if a value is greater than a value, if its greater then it returns true otherwise false<- inverse of>, it checks if a value is less than a value, if its less then it returns true otherwise false>=- same as>but also counts if the value is equal to<=- same as<but also counts if the value is equal to
to use the comparison operators you do this:
print(1 == 2)
it checks if number 1 is equal to number 2, you use this the same way you use the other operators:
print(1 == 2)
print(1 >= 2)
print(1 <= 2)
print(1 > 2)
print(1 < 2)
Operators | Compound Assignment Operators
Compound Assignment Operators are operators that are shorthand of basic arithmetic operations
if you want to increment a variable’s number by 1 you usually do this
local variable = 1
variable = variable + 1
but theres a shorthand of doing this by replacing variable = variable + 1 with
variable += 1
this basically gets the result when you add the variable by 1 and then returns the result to the variable so the variable’s value is now 2 after that
if you want to do subtraction you replace += with -=
summary of all compound assignment operators:
all of these get the result and returns the result to the variable
+=- shorthand ofExampleVariable = ExampleVariable + 1-=- shorthand ofExampleVariable = ExampleVariable - 1*=- shorthand ofExampleVariable = ExampleVariable * 1/=- shorthand ofExampleVariable = ExampleVariable / 1%=- shorthand ofExampleVariable = ExampleVariable % 1
Also the % operator basically returns the remainder of two numbers if they were divided
so you just replace your += with any kind of compound assignment operator in the summary list and it will get the result and return the result back to the variable
Ask any questions if confused