Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How does one do tables? (Dumb person alert)

Asked by 6 years ago

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?

0
how!? User#18979 5 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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
0
How would I make a PLAYER table? User#18979 5 — 6y
0
use game.Players:GetPlayers() OR game.Players:GetChildren() as they both return a table (an array) abnotaddable 920 — 6y
0
Thanks, one more thing: For the player table, how would I add players to a table? I would be checking the distance from a player to each of the players, then if they are close enough it should add to a different table like nearby or something... User#18979 5 — 6y
0
And what do you mean by "recall" o.o User#18979 5 — 6y
0
just say the line again i guess abnotaddable 920 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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.

Answer this question