Hey guys! Welcome to the final installment of Beginner Lua Lesson. After this, I believe you’ll be ready to start learning the more complicated stuff! Experiment! Read the Wiki! Read our other tutorials! Follow through with these tutorials, and in time you may become a Lua master! All that aside, let’s get started.
Introduction
So, what are tables? Tables are, in simple terms, a block of different values carried in one place that can be transferred between scripts. Tables have so many different uses; I don’t even know where to begin with their uses! But, to keep this simple, let’s show the basic uses and how to handle incoming tables from various service events. Read the ROBLOX Wiki for all of the events and callbacks from different Services/API’s.
Practical uses
Many common, useful functions (or methods) will yield tables. An example of this is GetChildren (or GetPlayers when gathering players). These will always throw back a table of all the descendants of the specified location. These will usually not have a specified name, so you will need to find a specific object/player based on its location in the table. To view how many objects are in a table, you would put a ‘#’ before the table name, for example: print(#players), after using GetPlayers, as it will tell you how many players are in the table. If you want to view what is in a specific location of a table, you would use: players[1]. This would pick the first player in the table. As we also learned with pairs loops, it will go through the table in descending order, with the ‘i’ or whatever letter you wish to use, tracking what position the object is in the table. Here’s an example of a script outputting every player’s name in order of which they appear:
local players = game.Players:GetPlayers() for i, plr in pairs(players) do print(plr.Name.." is player #"..i) end
Service events/callbacks that yield tables
Many services and API’s will include an entire table of variables; you should always read the Wiki to see what these variables are and what they serve. One good example of this is the callback ProcessReceipt, which is called by the MarketplaceService when a Developer Product is sold. You can read all of the variables of the table it throws in this ROBLOX Wiki article. It is a little more complex, so you will definitely need to experiment a bit so you can get it working before you release it to the public! Lucky for you, when playing in Solo Test Mode in ROBLOX Studio, you can purchase Game Passes and Developer Products without your account being charged! This is excellent for testing out purchasing DP’s and GP’s in-game!
Conclusion
Well, there isn’t really much more I can explain here. I cannot stress enough how much experimenting, trial and error, and blood, sweat, and tears goes into coding with Lua! And as always, never forget to read the Wiki for more in-depth tutorials! Lua is also constantly changing, so watch out for that! I hope these tutorials helped your knowledge grow, and as always, feel free to send me a message if you have any questions, comments, or concerns! Good luck with your coding endeavors!