Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to use table.insert with a key that's a string rather than just an integer?

Recently I've found out that you can make keys strings, which is super helpful for a lot of things, as well as super-readable, however, table.insert doesn't seem to like strings, and only works with integers.

Example:

-- Throws an error
table.insert(table, "key", variable)

-- Works fine
table.insert(table, 1, variable)

1 answer

✓ Accepted answer

You can just index the table, it's faster and better

table["key"] = variable

-- Supports all data and userdata
table[workspace] = variable

table["key"] -- variable
table[workspace] -- variable

There is no way to do that with table.insert if i am right, i probably am.

table.remove also only supports integers so you can use same tactic to remove elements from the table

table["key"] = nil

1 comment

0

So we assume that table["key"] is another table, since im inserting into it, and it has pre-existing data so = doesn't work, I solved this by doing table.insert(table["key"], variable), sorry I didn't make that obvious. However since I've got it solved, I'll mark this as correct, thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.