I’ve spent about an hour trying to figure out how to add a table into a preexisting table. I’ve tried doing table.insert(DataStore:GetAsync(player.UserId).TableName) and it hasn’t worked.
Here is my preexisting table:
As long as your table’s keys are all strings like they are now, you shouldn’t have any issues saving it. This shouldn’t really have much to do with datastores.
If you’re trying to insert a table into the "Cars" section of the table, you could do something like Template["Cars"][nameOfYourNewCarHere] = { ["Color"] = "neon pink or something idk lol"]} (specifically point out an index to set the table to rather than trying to insert it.) If you have two cars of the same name for some reason, you might want to change how you format cars within the "Cars" table since it will just over-ride the old value.
table.insert (which is what you were using) inserts your value at a numeric index. If you do it the way I showed (setting the value with a specified index), you won’t have this error.
The reason for this error is that in a datastore you can either save arrays (with indexes counting up 1,2,3) or dictionaries (only string indexes); you can’t save mixed tables.
Edit: Not random, it’s just put on the end. It’s still a number though.
Alright. I also need to save that data. And wouldn’t the table just be readable since it’s read from a datastore? Or would modifying it allow me to publish that new table?
It’s the fact that you’re table.inserting something into it which doesn’t let it be saved, that makes it have both string and numeric indexes. Instead of using table.insert, do what I said or something similar and set it to a specific index.
So when I do Template[“Cars”][“NewCar”], that creates new data?
Also how should I save this. Would SetAsync() with the entire datatable work or should I just try UpdateAsync() on the one value?
Template["Cars"]["NewCar"] refers to "NewCar" in the table. If you have something like the code below, the value of NewCar in the table will be the same.
local myTable = { ["NewCar"] = 7 }
local myTable2 = {}
myTable2["NewCar"] = 7 -- set the value of NewCar to 7
print(myTable["NewCar"] == myTable2["NewCar"]) -- both are 7
Saving it doesn’t work any differently, the only difference is that you can’t have both numeric indexes (myTable[2]) and string indexes (myTable["NewCar"]) in the same table. As long as all your indexes are strings (which is what doing this would accomplish) it should save the table exactly as it is.