Learn to Code via Tutorials on Repl.it!

← Back to all posts
Lua - Part 2: Tables
Opalmime

Hey, Vsauce. Opal here.

So, what's a table?

Well, a table in many languages is sort of like a dictionary..A bank of variables, if that makes sense. If you've ever done word banks in school, it's kind of like that!

How does a table work?

Tables work just like any type of code, but with lua, there's more than one way to create new variables for a table! That's where the management fun begins as well! As I show you how to give a custom variables for your VARIABLES! EXCITING!!! >:D

How DO I make a table?

Simple, let me make an example for you real quick -w-

How do I add stuff inside it?

Great! You just made your first table. Congratulations, my dear friend!! :D..However, how do I add stuff into a table? Well, there's 2 ways I know at the moment!:

I'm not saying this is a bad way to write and create variables into a table, but, I say there's a BETTER way of going about this! Here, let me make another example:

To me, this can look cleaner and easier to understand. Basically, think of the dot is your hand, and you're placing this fruit on the table! Easy enough, isn't it? Well..There's an even BETTER WAY to organise this code!! >:D (keeps getting better :p)

Adding multiple variables on a single line

You see here, What I done is just add 2 of the variables on the same line.. practically doubling as 2 variables as well, but into one! So, now I've got my 2 variables, I want to print them! Let's do that now..:

This is what the expected output is:

My hands hurt...

Great! We've made our code, we've made it print. You'd think this tutorial is over, correct? Well, not at all! >:)

Take a look. We DID make our variables, we've done what we wanted..But I mentioned how we can customise variables as well! So, let's do that:

It's JUST like the method I've shown in the last example I've made! However, since we're customising the variables (making a shorter version of it), we'll have to make another local, same logic as tables,but it's just new variables outside the table!

What can I make with this?

This is where the REEEALLL fun begins! So, let's make an inventory table real quick!:

Made the inventory, now, let's quickly make an input event! (it's easy to understand, if you're stuck just tell me in the comments! :3)

Expected Output:

Amazing! You made your very own (very very basic) shop system! I bet you're proud of yourself! ;p. Also, if you don't understand the whole if and elseif statements, then please let me know! I'm here to help after all! :3

What now?

Well, you've learnt all you need to know really about tables! I know it isn't much (again), but I'm doing it step by step so people aren't always getting confused while reading through paragraphs upon paragraphs and losing the motivation they had, etc etc.

No quote for this one, I think you can understand what to do for now! :3. I hope you all have a wonderful day, and happy October! 🎃🍬🍬

Part 3: https://replit.com/talk/learn/Lua-Part-3-Input-and-Output/147751

Voters
lajbel
TheDNAHero
Opalmime
Comments
hotnewtop
lajbel

Cool

samfierycreeper

@Opalmime Your first tutorial is called, "Lua - Probably One Of the ONLY Tutorials for Lua Out there.."

Which is a really sad, but a true statement. There really aren't a lot of Lua tutorials and it is a great language.

So even though I know all of this already, I really appreciate the effort that you're putting forward so that people can learn this underrepresented language.

Opalmime

@samfierycreeper Haha- Thank you man! It's not like I know a lot about the language, but I'm doing the best I can 😂

Opalmime

Woops! Ignore the math mistake I made in the shop system..I'm not very awake haha-

Opalmime