For once, I'm not asking for anything. I just want to know how the heck tables work.
Can somebody give me an empty table, and instead of values write what kind of value goes there? Or just explain tables to me because I don't get it. If I did I would have no problems with any of the sorts of things I'm posting here. How do I make a table, add to a table, and do something to every player in a table?
Tables are probably the most powerful thing in Lua. You can store any type of object string, anything pretty much, even more tables! e.g:
local newtable = {} --we use a variable and create an empty table using {} (note the specific bracket use, do not use () or [].
or if you want to create a new table with stuff already inside it:
local newtable = {"string", 1, false, {"another table"}, workspace.Part}
Note this is just an array (or a list). To add something to an array use table.insert
and to remove use table.remove
. e.g:
local t = {"string", 1} local x = Instance.new("Part") --create a new part x.Parent = workspace --parent last (efficiency) table.insert(t, x) --inserts x and the end of table t so table will be {"string", 1, x} --or table.insert(t, 1, x) --inserts x at specified position of table t so table will be {x, "string", 1} ------------------------------------------------------------------------------------------------------------------------ --to remove an element table.remove(t, 1) -- remove the element at specified position so if our table was {"string", 1} it will now be {1}
To see how long our table is or see a specific part of the table we can do:
local t = {false, 1, 2, 3, "string"} print(#t) --will print out "5" because thats how many object there are. print(t[1], t[3]) --will print out "false 2" because false is at position 1, and 2 is at position 3.
To loop through tables there are two methods:
local t = {"string", false, true, 9, part} for number, value in pairs(t) do --loops through the table t print(number, value) end --this will print out: --1 string --2 false --3 true --4 9 --5 part
or the other method:
local t = {"string", false, true, 9, part} for i = 1,#t do print(i, t[i]) end --this will print out the exact same, but it is your choice to what one you use
Dictionaries
dictionaries are tables but allow you to name the values(pretty much):
local d = { height = 50, age = 19, circles = 2 } -to change a value : d.age = 20 --to insert a value d.eyes = 2
EDIT:
local playertable = game.Players:GetChildren() for i, plr in pairs(playertable) do print (plr.Name) end --NOTE : You need to update the table by recalling it because when a player leaves or joins it is not going to affect that player or throw an error
Just note, I am answering his comment: "How would I make a PLAYER table?"
To make a play table you could define it like this
local playerarray = game.Players:GetChildren() -- From what I hear ':GetChildren()' automatically makes a table.
As for the rest, abnotaddable has proven a good answer on tables in general.
Don't forget to click answer on one of the answers. :) It helps both of us.