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

How to add table data to another table without overwriting it?

Asked by 6 years ago

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'] 
    }
    }
0
i did print(#Admins) and it says 0 so idk TheScriptKing 102 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

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
0
to so much dude TheScriptKing 102 — 6y
Ad
Log in to vote
-1
Answered by 6 years ago

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!

0
i do not think that is at all of what i am looking for ty thoe TheScriptKing 102 — 6y
0
Your asking to add table data so I assumed you wanted to add table data. hiimgoodpack 2009 — 6y

Answer this question