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

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

Asked by 3 years ago

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

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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
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. SpelunkyGaming 34 — 3y
Ad

Answer this question