The basic guide to learning scripting

Introduction
Ahoy Everyone, I’m FilmTune and I am going to show you my basic guide to Lua. I might not be the best scripter myself as I only have about 6 months of coding in Lua myself. But, I wanted to make a guide to help out the developers wanting to learn how to script. You also might need to know a little about studio before reading this guide, This guide covers only the basics such as.

  • Variables
  • Functions
  • Loops
  • If statements
  • Events

Now lets begin :smiley:

Variables

Insert your first script in server script service
image

Our first line of code should look something like this

local myNumber = 2

So to understand what local means its basically calling someone with local, your variable is the next line which you can name what ever you like, I named mine “myNumber” and myNumber = 2 so, that means the myNumber or the variable I created equals 2. You can call upon your variable later in the script so like this

local myNumber = 2

print(myNumber)

so now if we run the game “myNumber” or 2 will be in the output


you can also put strings instead of only numbers a string is a word and we right them like this.

local myString = "String"

print(myString) --prints your strings name in the output


you can also later change that variable you made like this

local myValue = false

task.wait(1)

myValue = true
Functions

here is how to create a function

local function myFunction()
		print("Cool Bird") --block of code goes here
end

myFunction()

lets break this down. So we make a variable, but we add function after local after that we then make a function name which can be anything, I named mine “myFunction” and then press enter so the “end” will appear the end means that we ended our function and in the middle between the “local function” and the “end” we put a block of code then we can call our function later in the script and when we call on it, it runs the code we put in the middle.

Loops

Now lets write a loop im guessing you understand what a loop is if you don’t it loops something you want to loop an infinite amount of times or a certain amount of times. There are two ways people can write loops the first way is using while true do.

while true do
	task.wait(1)
	print("loop")
end

we put while true do which loops an infinite amount of times, we put a task.wait() so that it dosen’t crash our game if we run the code with task.wait() you can put a number in the () which is how much time you want it to wait if you don’t put anything inside it will just do 0.05 seconds

now it’s time for, For loops. For loops are loops but you put how many times you would like to loop your code this is how you write them.

for myCount = 1, 5, 1 do
	print("This will be printed 5 times") --block of code
end

so we use “for” then we put our loop name which I used “myCount” we put our amount of times we want to loop it so we start at 1 then we put the amount of times you want to loop it then we go back to 1 then we use “do” then we put our block of code we want to loop a certain amount of times.

If statements

Now its time for if statements, If statements will check if a condition is met, we write them like this.

if 2 + 2 == 4 then
	print("2 + 2 = 4") --block of code
	
else
	print("Incorrect") --block of code
end

So how this code will work is If 2+2 == 4 then we would print that it does equal to 4 and if it dosen’t equal to 4 using “else” then it would print that it was wrong

Events

Events are events to see if the player has done something liked touched the part there are to many events to cover so I’m just gonna do the .touched event lets create a part in the workspace lets put that part a little above the ground and anchor it, create a script in that part

local part = script.Parent

part.Touched:Connect(function()
	part.BrickColor = BrickColor.new("Really red")
end)

So we call upon our part by using local, Script.Parent means the script’s parent is the part and the script is the child of the part, so then we write part.touched:connect(function() so we connect the touched event by using a function then we put a block of code in the middle so for the block of code I put I made it so if a thing has touched the part then the color of the part will change.

you can also put a pre existing function inside a connection like this

local function touchedThing(Part)
	print("Somthing has touched this part")
end

script.Parent.Touched:Connect(touchedThing)

instead of using the function we used the function we created with the event

So I guess you’ve learnt the basic’s if you would like a part 2 or if you would like me to fix a few things with this post then just tell me by replying. Okay bye :smiley:

What scripting level are you at?
  • Begginer
  • Intermediate
  • Advanced
0 voters
7 Likes

Tell me if this post helped you guys out and I might make a part 2

1 Like

Some nitpicking I have to say

You didn’t explain that you can change variables from their starting value.

You failed to mention that print and task.wait are also functions.

You also didn’t cover parameters for functions, or different usages of if statements(>, >=, <, <=, and, or, etc).

Lastly, not mentioning you can use an already made function for connections

local function Foo(Part )
    print(Part, "touched this")
end

script.Parent.Touched:Connect(Foo)

I think your loop section was done pretty decently, except for you saying if you don’t put something in task.wait(), it doesn’t do .1, it yields until the next heartbeat, which is generally around 0.05(wait() was around half the speed, so you probably just mixed them up)

Thanks for the feedback I will edit this post to fix my mistakes

Your basic guide is super cool! I already know these concepts, but I’d love having a part 2!

1 Like

Thanks for the feed back I’ll make a part 2 soon and add some more complicated stuff

1 Like