Scripting Basics - Beginner guide to lua and Roblox

I should have some extra content in by this weekend! I have been very busy with school so I haven’t had time to add the stuff I want to :confused:

5 Likes

Okay. Looking forward to it! :smiley:

5 Likes

I’ve searched for how to use for i,x in next() do on the developer hub, how do you use it?

2 Likes

https://developer.roblox.com/en-us/api-reference/lua-docs/Lua-Globals
See this page here. It contains the documentation for next.

5 Likes

Actually I’ve updated my post! It now has for loops including next loops and other iterator function loops! :smile: Hopefully this shows you how to use next.

Next is also explained here briefly: https://developer.roblox.com/en-us/articles/For-Loops

5 Likes

I’m new and u lost me at for loops, could you give more of an explanation, please? Thanks.

1 Like

What is it do you find confusing on loops?

1 Like

Rhanks for replying. I dont really get this bit ^^

Alright. So what this is loop for is iterating over something (It could be items in a table, Children of a Model or workspace, It could Descendances of a screen GUI, the possibilities are endless. Let’s break the code up a bit

Index

index and value, what is this supposed to be? Well let us start off with Index first. So the Index is just the position of where the value is in the thing we are iterating over. Same shows in Python and JavaScript as well. The first item in our iteration will always take the index of 0 Take a look at this example

myTable = {"My","Name","Is","John") -- This is our table

for i,v in pairs(myTable) do
    print(i) -- This prints the value of where the Index is at.
end

Our table is myTable and contains 4 different pieces of information. My, Name, Is, John. Now our first Index will be My at the Index of? If you said 0 you are correct

I could be wrong but for most programming languages the first Index of a table will ALWAYS be 0.

So if we continue our program our expected output will be
0
1
2
3
We have 4 values but since 0 always takes the first index it will print only up to 3.

Values a.k.a v

So now we know what the i is for, let’s dive into what the V is for. Always think like this
Key = i, Value = v ; Key Value Pairs

So now that we know what the value of where to access the Value which is the index, how about we learn what is the value.

Simple The value is just what the index is assign to. So My, Name, Is, John are all values of the table aka the Value. So when I said Key value pairs the I is the Key to access and the Value is just what it is assigned to and there is 2 of them making it a pair. Hence the term Key, Value pairs.

Now the parameter. As you saw I passed myTable through the parameter of pairs. Why? That is because the program has to know what we want to even iterate over. We can iterate over anything. In this case we are iterating over a table. We can iterate over Children of a table. Like this


folder = script.Parent
FolderChildren = folder:GetChildren

for i, value in pairs(FolderChildren) do
    print(value.Name)
end

This will print all the names of The Children of the Folder.

When to use

Normally I’ve seen people use this if they want to iterate over something that appears a lot instead of writing a seperate variable for each seperate Child which is DRY Code.

Hopefully this helps clears up somethings on for i,v in pairs loops :smile:

6 Likes

Woah! Thank you for taking so much time just to explain one thing to one person. Your such a good inspiration. I’m glad i replied at the right time!

1 Like

I am so happy I could help! There could be some few things that I could’ve explained better :smile: but it’s decent to get you started! If ever in doubt just shoot me a DM I can help!

2 Likes

Man that’s literally the best tutorial i have seen. The fact that you took the time to go through and explain this makes me very happy. I’m glad that you feek lime you have achieved something by helping someone. I followed you and will make sure to read all your posts. It’s 11pm for me so i might go to sleep now but you have been a MASSIVE HELP. Maybe one question: What is a “descendant?” Sorry for being a hastle. If it is an explanation longer than a sentence dont worry ill just find an article

2 Likes

So say you have a a Screen GUI. The Inside the screen GUI you have a TextLabel. Now this TextLabel will be the child. So then say We have a TextButton inside of the TextLabel. This becomes the Child of the Text label but is know a descendent of the Screen GUI So think of it like this

Screen GUI - Mother or Father of TextLabel

TextLabel - The Son or Daughter of Screen GUI

TextButton - Son or Daughter of Text Label AND a Descendant of the Sceen GUI almost like a far down member of a family tree

3 Likes

Oh thanks. So basically the textbuttin is a grandchild in this situation. Nice! I wont bug you anymore. Have a nice day or night depending on ur timezone (i love how diverse this developing community is)

2 Likes

Sorry for the bump, but Lua arrays start at 1. (Most other languages start at 0.)

3 Likes

Thanks man! I wasn’t sure about this part but I decided to go with the majority rules :D. Thanks! I will edit my ultimate post.

2 Likes

I haven’t updated this in quite a while, and, I decided to come back to it and fix some issues. I fixed a few places where wording was misleading, and fixed a few typos, as well as making things a bit easier to understand.

I’m going to update it more in the future, and try to shorten some of its longer parts down, so its easier to read. I might expand this to include info about Roblox Studio as well. :smile:

You can find an original version of the article here: (Original 2019 article) Scripting Basics - Beginner guide for scripting on Roblox

4 Likes

Hi I know this is extremely late but thank you I think I can use this because I don’t know how to script, not to mention I also became member a few weeks ago so this helps a lot

2 Likes

This honestly seems like a great guide, even though I haven’t quite read it all but it seem pretty simple to follow! I can watch a podcast while reading this and I can also learn at the same time.

1 Like

Hey so I still can’t create posts on the forum yet and i dont know where to go for help.
I am new to scripting and I have this scipt (below) that basically is just a round system that chooses a map to teleport players too but I am not sure how to make it so it teleports all players to the same map. Any Help is appreciated thank you.

VL script

1 Like