( part 1 ) Basics of scripting - guide on common codes used

( I am not sure if these are called “codes” or not but some people have been asking me about basic common codes used on Roblox, and I decided to try to make a tutorial here :man_shrugging: )

note: this tutorial won’t go through everything, it is just the most common and unexplained codes ( in my opinion ), I’ll make more parts if you guys think this is not bad

What this tutorial will go through common codes used and how they are used
tutorial starts here

Basics of scripting - guide on common codes used

For loops:

For loops are loops that only runs for a specific amount of times.
For loop code:

for variable_name = 1,3,1 do
	print("ahh")
end

What this does is that it loops 3 times ( from 1 to 3, 1st time, 2nd time, 3rd time, and having a increment of 1 everytime, 1, 1 + 1 = 2, 2 + 1 = 3 ). Now you may be confused by the “variable_name”. It is just a variable name for the loop. ( You can name it to whatever you want, it is required for the loop to work ). Note that the increment is not required, you can leave it blank and roblox will set it to 1 ( default )
If you run the code:


It prints “ahh” three times!

You can even add a wait(1) in the loop:

for variable_name = 1,3,1 do
	print("ahh")
        wait(1)
end

You can probably guess what it does, It prints “ahh” 3 times, but waiting 1 second before printing each time.
And you are right!

Example of use of for loop:
Let me take a commonly used example of for loops: timers!
You may think that the “variable_name” I talked about which is required for the loop to work and run is useless. Actually its not.

for timer = 1, 10,1 do
	print(timer)
	wait(1)
end

In this code, I set the variable_name 's name to timer ( since we’re making a timer program, you can name it to whatever you want though ). What the timer does ( except required for the loop to work ), it is a variable that has data of the 1 to 10. It may seem a bit complicated at first, but lets test the code above to see what it does.


It prints this, it actually prints numbers from 1 to 10, just like our loop which loops 10 times ( from 1 to 10 ), and waiting 1 second before printing the next number everytime ( since we added a wait(1) in the loop ).
You get what I mean? You can literally make a whole timer out of this. You can change the increment too, like

for timer = 1, 10, 0.1 do
	print(timer)
	wait(1)
end

now it prints this:


sheeeesh, it prints from 1 to 10, but this time it doesn’t do it 1 by 1, its 0.5 by 0.5 ( 1, 1.5, 2, 2.5… )

That is all for the part For loop, now you know how to use it and I hope it helps you with your programs or projects.

For i, v in pairs:

I don't know if this sounds confusing to you or not, but I definitely got stuck here for a bit of time ( idk if you got stuck, maybe its just me 🥲)

code format:

for i, v in pairs() do
	
end

We just talked about for loops, what for i v in pairs() does is that it runs through everything in a table ( A variable can only store one data, but a table can store multiple datas, must be in these brackets {} ). Its like for loops but the number of times it loops through is the number of datas in the table. Kind of like

for loop = 1, 10, 1 do
	
end

but the 10 is set to number of datas in the table.
The cool part is that the i and v are variables which means you can set it to whatever name you want ( just like the variable_name we talked about in for loops )
Lets take this code as an example

code example:

local table = {"lol", "don't be toxic", "hello", "its christmas"}

for i, v in pairs(table) do
	print(i,v)
end

We created a table called “table”, and with datas “lol”, “don’t be toxic”, “hello”, and “its christmas”. Now what the for i, v in pairs() will do is that it will run through every data in the table we chose ( which you have to put it in the brackets in pairs() . The i means index, v means value. In this case, if i is 1 then the value is “lol” ( the first data in the table ), if i is 2 then the value is “don’t be toxic”. You get what I mean.
Now lets run the code and see what it does


It prints this which is what we want ( since we put print(i,v). The first line, the i is 1 so the value is “lol” as shown in the table. print(i,v) means print index and value, you can just do print(v) or print(i), its up to you ).

Now let me talk about a use of for i, v in pairs()

Value check
Yes, value check. For example in the above code, we can check which value in the table equals to something, and if it is equal to that thing then we print something. Lets try it out:

local table = {"lol", "don't be toxic", "hello", "its christmas"}
for i, v in pairs(table) do
	if v == "don't be toxic" then
		print("Yes, that is true. Don't be toxic.")
	end
end

In this code, it runs through the whole table ( which it should run 4 times since there is 4 datas in the table ). And everytime it checks if the value is equal to “don’t be toxic”. If it is, it prints “Yes, that is true. Don’t be toxic.” ( never be toxic, ok ). Lets run the code:


This time, it doesn’t print out the whole table like last time since we didn’t put print(i,v) . Instead, when it sees the value is “don’t be toxic”, it prints “Yes, that is true. Don’t be toxic.”

With for i, v in pairs(), you can do a lot of things. Like changing transparency of all parts inside a folder

for i, v in pairs(game.Workspace.Folder) do
	v.Transparency = 1
end

( this changes every value inside the folder “folder” 's transparency to 1 )
you can check what the value type is, too

for i, v in pairs(game.Workspace.Folder) do
	if v:IsA("Part") then
		v.Transparency = 1
	end
end

See how useful for i, v in pairs() is!!!

That is all for for i v in pairs, and also the end of this tutorial part 1

If you guys think it is useful for beginners ( or at least its not bad… ), leave a comment, if you guys think it is good or ok, I’ll try to make another one ( part 2 ).

I hope this helped!

7 Likes

why did you write smelly wait!1!1!!1

replace it with task.wait

also the ipairs is faster than pairs

You don’t need pairs for that, just

local mogus = {"sussy baka","nodrip"}

if table.find(mogus,"sussy baka") then
print("thads sum driip!1")
end
2 Likes

not anymore it isn’t - the difference is negligible now

Overall, I think this was a good guide. However, it was a little hard to read. I think the print statements were too long and there were too many sentences in parentheses so it made it hard to tell what I should focus on. I also think the amount and use of parentheses made you seem less credible. The output also had all the time logs making it a little harder to read. A very minor issue I had with this was naming conventions. Usually when people use pairs() they use k, v in pairs() because it’s usually used for dictionaries. Also, not many people use snake_case when naming variables in Lua. Thanks for posting though, I’m sure people found it helpful.

1 Like