Im sorry, this doesnt make a whole lot of sense, but I am going to try to explain it.
So pretty much, I want to save some data in a table. The data is a list of user IDs, and each user ID will be the key for a list of tags, and each tag will have a number attributed to it. Like this:
Template:
data = {
[userId] = {
[tag] = 0
[tag] = 0
[tag] = 0
}
}
Example:
data = {
[4836512632] = {
["abc"] = 10
["def"] = 5
["ghi"] = 2
}
[1682535474] = {
["abc"] = 24
["def"] = 1
["ghi"] = 9
}
[9634548525] = {
["abc"] = 6
["def"] = 92
["ghi"] = 12
}
}
The problem is, I need my code to name the keys in the dictionary, so that a user can give me a list of tags and a user ID, and the code will create a new user ID table (shown in the template section) inside the main table (called data
), with the word userId
replaced by the user ID, and each of the words tag
replaced by the tags given by the user (there may be more or less than 3).
I have no idea how to do that, it might be possible to use string formatting (that is what I tried) but I don’t know how to make it work. Is it even possible to change/create a dictionary key with code?
I tried this: (does not include a way to make it work with any number of tags)
tags = {"a", "b", "c"}
dataTemplate = {
["%c"] = {
["%s"] = 0,
["%s"] = 0,
["%s"] = 0
}
}
data = {}
userId = 1234567890
table.insert(data, string.format(dataTemplate, userId, tags[1], tags[2], tags[3]))
print(repr(data)) --repr is a module that prints tables in a readable format (link below)
Repr – function for printing tables - Resources / Community Resources - DevForum | Roblox
The (first of many) problems is, I get an error because I cannot do string.format()
on a table. The way to get around this would be to do string.format()
on each of the dictionary keys, instead of on the table as a whole, but I dont think you can reference the keys in the code. I know you can change the values, but I dont think the keys.
So, because I don’t know how to change what the key is, I decided to change it to something like this:
tags = {"a", "b", "c"}
dataTemplate = {
{"%c", "%s", "%s", "%s"} -- %c is for the userId, %s is for tags
}
data = {}
userId = 1234567890
table.insert(data, string.format(dataTemplate[1], userId, tags[1], tags[2], tags[3]))
print(repr(data))
This doesnt work, because again, I am giving string.format()
a table.
To solve the problem of giving string.format()
a table, I tried this as a test:
tags = {"a", "b", "c"}
dataTemplate = {
{"%c"}
}
data = {}
userId = 1234567890
table.insert(data, string.format(dataTemplate[1][1], userId))
print(repr(data))
When I did what is in the dropdown above, I got this in my output: {"ďż˝"}
. repr will print tables inside tables, so the ?
isnt because it is being asked to print a table in a table. I confirmed this by doing print(repr(data[1]))
and it gave me "ďż˝"
.
Any help would be greatly appreciated, and I understand if you don’t want to help, it is a lot of stuff to look through, and this is only the beginning of what I am trying to do. I really hope I explained it well enough, and please lmk if you need clarification. TYSM!