Forum > Coding >

[Tutorial] Tables in Lua

Posts: 250

Threads: 111

Joined: Jun, 2018

Reputation: 5

Posted

My explanation on tables may not be detailed or as easy-to-understand as the actual wiki. So here's the actual wiki link:

https://www.tutorialspoint.com/lua/lua_tables.htm

 

Tables are very important and useful in lua as they store multiple values. Below are some examples of tables you could use:

Whitelisted table

Blacklisted/Banned table

Admins table

Players table (Actually just the game.Players:GetPlayers() function)

UserId table

 

Let's get started.


First of all, tables are stored within curly brackets ( "{" and "}" ). Unlike other "tables" in other languages such as CS or Java, the first index is always "1" instead of "0".

For example, table[0] is invalid as it starts out as table[1].

 

To store stuff within a table, type out a variable like this:

local admins = {"JesseMan", "JohnnyDoe", "CanadaLord"} --1, 2, and 3.

print(admins[1]) --JesseMan gets printed

 

Now you can just call them however you want! Of course, you should know of the actual functions of tables.

Below 2 of the most important functions will be explained (table.insert & table.remove):

table.insert(table, (optional: position), string) - Adds another string to a table

table.remove(table, position) - Removes a table by position

 

Here is an example of how it works:

local admins = {"JesseMan", "JohnnyDoe", "CanadaLord"}

print(admins[3]) --Prints out "CanadaLord"

table.insert(admins, "StanTheMan") --Adds "StanTheMan" at the last position (4)

print(admins[4]) --Returns "StanTheMan"

 

table.remove(admins, 4) --Removes the 4th position from the admins table ("StanTheMan")

print(tostring(admins[4])) --Returns as nil

 

Hooray! You now know how basic tables work and of the two most used functions!

Lastly, an important thing you should know is "for i,v in pairs(table)" which repeats the code until it cycles through the entire table.

Example:

local admins = {"JesseMan", "JohnnyDoe", "AnimeMan2000"}

local str_admins = "" --Blank string

for i,v in pairs(admins) do --Cycles through the table admins

     if (i == 1) then --If first cycle

          str = v

     else --If i is not 1

          str = str..", "..v

     end

end

print(str_admins) --Returns as "JesseMan, JohnnyDoe, AnimeMan2000"


I hope my tutorial helped you out with tables or taught you about tables. Of course, if you need extra information on tables go to the top of this post as linked there is the lua wiki about tables.

  • 0

Exploits I own: Synapse, Electron

Scripts I've made: Aimbot GUI, Draco Admin

Scripts I'm working on: More game ESPs

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )