How to teach yourself Lua
Posted on July 28, 2016 by Perci1
The ability to teach yourself is one of the most important skills you will ever learn. It allows you to learn not only independently, but also at a much faster rate. This ability will ultimately put you far ahead of your peers, and can separate an average Joe from a genius.
But lucky you! If you're reading this, you’re most likely already ahead of the game, assuming you want to go into programming. Don't get bogged down by all the super smart people on Scripting Helpers. This is a minority. The mere fact that, as a kid, you're taking an interest in programming is amazing, and will put you ahead of many other future programmers.
My father is currently a programmer, but he had absolutely no experience before his first class in college. Why? Because nobody had a personal computer back then. (I know, it was like the stone age or something). But now we have an actual game that makes programming fun, as well as a massive amount of documentation on the subject. We just need to take advantage of it. So, here are some tips about teaching yourself Lua.
Keep it simple
I know, I know. You want to make an FPS with fire-breathing dragons that fly around destroying buildings. But you can't have the mindset of wanting to learn that, because there's not really anything to learn, per se. It's a project, not a specific skill. You need to learn Lua, not complicated projects created with Lua.
Always start as simple as possible. There's actually a whole blog post that goes more in-depth about simplifying your problem, so be sure to check that out.
Research it
It's not that this is super hard, but a lot of people just don't do it. The Roblox wiki has a ton of information. Aside from their tutorials, which are great, it has documentation on all the properties, events, and methods of all objects.
Maybe I want something to happen when my mouse enters a GUI, but I have no idea where to start. All I have to do it look up Frames on the wiki, look under events, and I'll see a MouseEnter
event. There. Problem solved. But yet so many people don't do this.
Google is also helpful. Of course, it's more of a hit or miss with google. It can be helpful to put "for dummies" at the end to get a simpler explanation. When I was confused about Bézier Curves, a tutorial I found with google explained it to me very well.
Mess with it
This is by far the most important section of this entire post. And yet it's also what people skip more than anything else. We are far too prone to desire a quick and easy answer to our problem. While outside help certainly has its place, so does messing with code yourself.
Sometimes it's hard to actually take the time to write out code. If you understand it, you'll want to move on to the next tutorial. But the only way to get better, to truly learn how something works, is to practice it. Practice may not make perfect, but it does make progress. It doesn't matter how well you think you understand a concept, write it out, change it, mess with it. Force it to error. Then fix that error! Think of another way to do something. Switch around a value and see what happens. This is how you will improve.
Use print
constantly. Seriously, people totally underestimate the usefulness of this function. Can't get your CFrame to work? print
it, see what's going on. "Oh! I changed the X axis instead of the Z axis!" Not sure where a script is breaking? Use print
to find where it stops working. "My event isn't firing!" The point is, use print
all the time.
Application
Let's go through an example. Perhaps I want to learn about generic for
loops.
local greetings = {"Hello", "Hola", "Bonjour", "Guten tag"} for i, v in pairs(greetings) do --stuff end
Okay, so we start with a table. I already know about tables, but if I need a refresher, a quick wiki search will solve any confusion.
But what is pairs
? Let's look it up. Nope, that page doesn't go into much detail, and I can't totally understand it.
Let's just look up the generic for loop page. Okay, so pairs
is an iterator function. I may understand their example, or I may not, but the concept makes sense. It just returns whatever is next in a list each time it's called. So the first time an iterator function is called, it might return 1. Then if it's called again, it might return 2, then 3, etc.
Scrolling down, the page gets more specific. "[pairs will] return table keys and their corresponding values." Okay. So each time it's called, pairs
will return the next key/value pair in the table. Now it makes sense why it's called pairs.
Testing
We actually could have skipped a lot of the research if we went straight to this step. It would ultimately be limiting, as I wouldn't really understand generic for
loops. But I would be able to use them, even if I didn't understand. That's how powerful experimentation is. And if I didn't understand the research, testing would help me understand.
local greetings = {"Hello", "Hola", "Bonjour", "Guten tag"} for i, v in pairs(greetings) do print("i = "..i, "v = "..v) end
The result:
i = 1 v = Hello i = 2 v = Hola i = 3 v = Bonjour i = 4 v = Guten tag
Okay, so this makes sense. i
must be the index of the table and v
must be the value.
But what about this? I've seen people do this before.
local greetings = {"Hello", "Hola", "Bonjour", "Guten tag"} for _, v in pairs(greetings) do print(_, v) end
1 Hello 2 Hola 3 Bonjour 4 Guten tag
Alright, so it didn't make a difference. Perhaps I can name i
and v
anything I want?
local greetings = {"Hello", "Hola", "Bonjour", "Guten tag"} for flopperderf, buggybaffles in pairs(greetings) do print(flopperderf, buggybaffles) end
1 Hello 2 Hola 3 Bonjour 4 Guten tag
Great! So I can name them whatever I want. What happens if I leave out one of the variables?
local greetings = {"Hello", "Hola", "Bonjour", "Guten tag"} for v in pairs(greetings) do print(v) end
1 2 3 4
So if I leave out one of the variables, that's fine, but the first one is always the index of the table. Therefore if I want the value, I have to have both. What if I leave out both variables?
local greetings = {"Hello", "Hola", "Bonjour", "Guten tag"} for in pairs(greetings) do print( ) end
11:50:18.457 - Workspace.Script:3: '<name>' expected near 'in'
Nope, that doesn't work.
This is the process of learning by messing with code. It's just a constant series of What if I do this? What if I do that? combined with using print
a bunch.
But... How do I start?
As you've probably noticed, this article was focused mainly on people who already have some experience, helping them increase their knowledge. If you have absolutely zero scripting experience, it's even easier! There's a massive number of tutorials for people with no experience.
Intro to Scripting, Scripting Cookbook, CodeTheorem's Intro to Scripting, ScriptGuider's Lua tutorials, Roblox's Videos, Roblox University, and Roblox's Hour of Code are great tutorials that will teach you a ton.
Even still, you won't improve if you don't practice.
Conclusion
Scripting Helpers can't do everything for you. There's a point where you just gotta sit down and teach yourself.
1. Keep it simple. Don't take on something two complex for you to handle, learn step-by-step.
2. Research it. There's no need for you to ask "Uhh, what was the property that did X?" Because all that information is on the wiki, and you can look it up easily enough.
3. Mess with the code. You won't get anywhere or learn anything if you neglect this. Use print
constantly.
Anyways, I hope this article was helpful. Keep learning :)
Commentary
Leave a Comment