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

How do you really access a table? [i dont need sleep i need answers]

Asked by 4 years ago
Edited 4 years ago

Uhmm, so you might recognise me from the dialogue thingy but ummmm.

I got an error saying; Players.maxpax2009.PlayerGui.ScreenGui.LocalScript:15: attempt to index nil with 'Name'

How do you really acess a table?

local PartsToDialogue = {}

for i, v in pairs(game.Workspace.DIalogues.Models:GetChildren()) do
    table.insert(PartsToDialogue, v)
end

print(PartsToDialogue.v.Name) --Attempt to index nil with 'Name?'
0
hewllo spammer Nguyenlegiahung 1091 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago

There Are 2 Types of Tables

  • Lists

  • Dictionaries


Lists

local listTable = {"a","b","c"}

Now this is a regular table that holds these 3 values as Strings.

If we wanted a certain value from this table, we would have to Index it with [ ]

For Example, if we wanted to get b from this table we would do...

print(listTable[2])

The result of this would print b in the output because we are indexing or getting the second value that is in the table.

We can also do this with for loops, like so...

for i = 1, #listTable, 1 do
    print(listTable[i])
end

This will print all the values in the table because we are starting at 1, to the number of items in the table, which is 3, by the increment of 1. So, the result of this will print a b c.

This is just doing the same thing three times, just simpler...

print(listTable[1])
print(listTable[2])
print(listTable[3])

This is a bad habit and inefficient, it would be better to do a for loop.

Another cool thing you can do with indexes is doing these for children of an object!

local items = game:GetService("Workspace"):GetChildren()

for i = 1, #items, 1 do
    print(items[i].Name)
end

This will print the name of all the children inside of Workspace, since we are using the same thing we did for the listTable.


Dictionaries

Now dictionaries are like lists, just with subtables inside of the values.

local newDictionary = {
    ["Value1"] = {
        ["Green"] = {0,255,0},
        ["Red"] = {255,0,0}
    },
    ["Value2"] = {
        ["Blue"] = {0,0,255},
        ["Black"] = {0,0,0}
    },
    ["White"] = {255,255,255}
}

This is just a table inside of a table holding multiple values of tables!

Dictionaries have unlimited possibilities, especially when you encounter metatables!

So, if you wanted to get the value of Red you would have to index the dictionary with [ ]...

local redDirectory = newDictionary["Value1"]["Red"] -// This will return the table, so we'll have to index the table that the value holds

print(redDirectory[1]) --// Results in printing 255, as that's the first value in the table.

Most of the things you do with listing tables, you can do with dictionary tables!

Possibilities are endless!


Hope this helped! Feel free to select this as an answer if this helped you!

0
Woah this is interesting! Thanks for going in-depth, i cant wait to learn meta-tables! maxpax2009 340 — 4y
0
No problem! Glad to help! Meta-tables are very useful when you have a value that doesn't exist, but a sub-table to substitute the missing or nil value! killerbrenden 1537 — 4y
Ad
Log in to vote
3
Answered by
synkrio 281 Moderation Voter
4 years ago

You are trying to get the values in a table by using the value which is not going to work. Tables have an index assigned to every value in it. Doing table.insert gives a numeric index to the value. By doing PartsToDialogue[1].Name, you can get the name of the first value in the table.

0
But what if i want to use them all? i need to assign variables for them? maxpax2009 340 — 4y
1
just do for i,v in pairs(PartsToDialogue) do print(v.Name) end synkrio 281 — 4y
0
If you're trying to get the values inside of a table, and you know the directory; you will have to index using [ ]. Along what synkrio has said, tableName[firstPart][secondPart][etcPart] killerbrenden 1537 — 4y
Log in to vote
-2
Answered by
XDvvvDX 186
4 years ago
Edited 4 years ago

I can't explain it to you if you don't want to 'sleep'.

0
I just need help, no jokes okay? :) maxpax2009 340 — 4y

Answer this question