I have heard about tables but i don't know what they are used for and how to make one?
A table is used for creating arrays. In that wiki post, it gives a better explanation. Let me provide you with an example:
My_Table = {"Shawnyg", "Shedletsky"} -- My Table(array) script.Parent.Touched:connect(function(hit) -- Touched event if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then -- check if it's a player p = game.Players:GetPlayerFromCharacter(hit.Parent) -- sets the variable for i,v in pairs(My_Table) do -- accesses the table if p.Name:lower() == v:lower() then -- if the person's name is from my table, then m = Instance.new("Message", Workspace) -- make a message m.Text = p.Name.." has touched the door!" wait(1) m:Destroy() end end end end)
Tables are created with an open and close brace like this:
local myTable = {}
It is possible to group many variables into a single table. They can also be of any type (bool, int, float, string).
local myTable = {1, 2, 'uno', 5.3, 'alpha', 'What am I doing?', true}
An extension of tables are dictionaries. Dictionaries in code have a key associated with a value and can be written like so:
local myDictionary = { ['FirstValue'] = 1, SecondValue = 2, --this is shorthand for string keys oneOfMyFavoriteThings = 'applesauce', aFloatingPointNumber = 5.4, whyAmIDoingThis = 'Dictionaries are fun.' [102] = 'My Worst Class Ever', --keys can be anything, not just a string [game.Workspace.part] = 'Can this go on any longer?'}
A benefit of writing tables like the above is that RobloxLua lets one collapse the table to save visual space. Handy when those tables reach many tens of lines long.
Accessing a value from a table can be slightly confusing to begin with. It is done like this:
print(myTable[1]) --output: 1 print(myTable[3]) --output: uno print(myDictionary['oneOfMyFavoriteThings']) --output: applesauce print(myDictionary['aFloatingPointNumber']) --output: 5.4
It is also fairly simple to write a value to a table.
local mySecondTable = {} mySecondTable[1] = 'hello'
And finally, a very useful thing in many games, is iterating over a table.
local Banned = { 'Telemon', 'Guest', 'EVERYONE', 'maniacs'} game.Players.PlayerAdded:connect(function(newPlayer) for k, v in pairs(Banned) do if newPlayer.Name:lower() == v:lower() then newPlayer:Kick() print('Banned player' .. newPlayer.Name .. 'has been kicked from the server. Have fun with tables!') end end end)
Feel free to comment with any questions. Don't forget to accept this answer and/or up vote if you found it helpful. Thanks!