Scripting Basics - Beginner guide to lua and Roblox

I more meant focusing on Roblox-specific concepts more than lua-specific. For example there tends to be a lot of confusion specifically related to Roblox events.

Also your reference to _G does seem like something that might go well in this post. _G (in specifically Roblox) simply acts as a table shared among all scripts in the same environment (basically server/client)

_G is actually not even special compared to a table, it’s literally just a shared table which is completely different to vanilla lua.

In vanilla lua, _G represents the global environment of all scripts, so when you use print you’d technically be using _G.print. (That’s actually one reason why lua is used as a missing language, it offers full modification of all functionality including built in lua functionality)

4 Likes

My personal methodology is based on the C#7.2 standard since C# is the main language I use. You may see it fit to go over what Pascal case and camel case are. Usually they are easily portrayed by “PascalCase” and “camelCase” since writing it like that reflects on how the values look.

My sort of “translated standard” for Lua and what I use personally is:

local PrimaryValue = "This value is in the root of the script, so it's PascalCase"
local THIS_VALUE_IS_A_CONSTANT_SO_IT_IS_ALL_CAPS = 0xDEADBEEF

function FunctionNamesArePascalCase(paramatersAreCamelCase)
    local andVariablesInFunctionsAreCamelCaseToo;
end
----------------
local MyModule = {}
-- Lua pseudo-constructor
function MyModule.new()
    local object = {}
    object.PropertiesAndMethodsArePascalCase = true
    return object
end
return MyModule
8 Likes

Very nice tutorial! I am seeing myself in the future using this. Bookmarked too!

(Unrelated but this is my official 2nd post!)

6 Likes

That’s great! :smile: Welcome to the devforum!

5 Likes

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