i am working on an chat system. i got the chat working great but i am having a little trouble with adding table data to an table i got in the module script?
ChatHandler = require(script.Parent.MainModule) Admins = { ['ScripterOmega']={ ['tagColor'] = ChatHandler.colors['white'], ['chatColor']=ChatHandler.colors['black'] } } ChatHandler.Admins = ChatHandler.Admins[Admins] print(ChatHandler.Admins['ScripterOmega']['chatColor'])
-- tihs is the table in ChatHandler module scrtipt ChatHandler.Admins = { ['Server']={ ['tagColor'] = ChatHandler.colors['red'], ['chatColor'] = ChatHandler.colors['green'] }, ['Owner']={ ['tagColor'] = ChatHandler.colors['green'], ['chatColor']=ChatHandler.colors['red'] }, ['Chat']={ ['tagColor'] = ChatHandler.colors['blue'], ['chatColor'] = ChatHandler.colors['green'] } }
So there is an important distinction to make here. The module script has a table of keys (sometimes this is called a hash table). Functions like table.insert won't work here to my knowledge because they work with indices (indexes) which are just what position the element in the table is.
That will actually explain your comment of why something like ...
x = {['one'] = 1} -- is a valid table print(#x) -- > 0
will print zero. Because it is counting the number of positions in the table (number of indices! commonly known as the length), and we only have keys, then it will return zero.
To add something to the table, we will need to define a new key. Here's how I would do it in your script. I noticed you tried to do this with what you posted, but we need to do it in a very specific way.
ChatHandler.Admins['ScripterOmega']={ ['tagColor'] = ChatHandler.colors['white'], ['chatColor']=ChatHandler.colors['black'] } }
That will add a new key. If you want to add multiple new keys you will need something to go through your table to do that.
admins = { ['exampe'] = {['kools'] = 'kool guy'}, ['exampletwo'] = {['me'] = 'kool'} } for key, admin in pairs(admins) do ChatHandler.Admins[key] = admin end
You can easily get this done with table manipulation. Here is a easy way to get the alphabet.
local alphabet = {} for letter = 97, 122 do table.insert(alphabet, letter - 96, math.char(letter)) end for _, letter in pairs(alphabet) do print(letter) end
If you wanted to see what I did, see here)
Hope this helps!