Greetings everyone. Directly to the point, I am trying to save a table with DataStore and I am getting this error:
Cannot convert mixed or non-array tables: keys must be strings
Here is my code. (NOTE: It's translated for you to understand better, but you can be sure language is not the problem causing this error.)
local ArmyFolder = game.Workspace.Armies for a, b in pairs(ArmyFolder:GetChildren()) local ArmyID = b.Settings["ArmyID"].Value Armies[ArmyID] = {} Armies[ArmyID]["StartX"] = b.Settings["Start"].Value.X Armies[ArmyID]["StartY"] = b.Settings["Start"].Value.Y Armies[ArmyID]["StartZ"] = b.Settings["Start"].Value.Z Armies[ArmyID]["EndX"] = b.Settings["End"].Value.X Armies[ArmyID]["EndY"] = b.Settings["End"].Value.Y Armies[ArmyID]["EndZ"] = b.Settings["End"].Value.Z Armies[ArmyID]["StartTime"] = b.Settings["StartTime"].Value end
I spent hours(Literally hours, like 7-8 hours) looking for the problem and trying every combination I could try... I really need help. Thanks already!
You actually can put tables in tables:
> ds=game:GetService("DataStoreService") > d = ds:GetDataStore("Debug") > d:SetAsync("debug", {1,{hi=2, there=3},4}) > t = d:GetAsync("debug") > print(unpack(t)) 1 table: 3402BC9C 4 > print(t[2].hi, t[2].there) 2 3
The problem is that you aren't allowed to have a list with holes in it (that's what it means by "non-array tables"). Actually, it won't error for that (I tried it - you lose information, but it won't error), but it will error if you have integer keys without there being a key in the first slot. ex it will error if you try to save t = {}; t[2] = 2
Note: RemoteEvents and RemoteFunctions have the same limitation
To fix this, you need to store a list of armies, not a dictionary of their ID to the values. That is:
local ArmyFolder = game.Workspace.Armies for a, b in pairs(ArmyFolder:GetChildren()) do local army = {} table.insert(Armies, army) army["ID"] = b.Settings["ArmyID"].Value army["StartX"] = b.Settings["Start"].Value.X army["StartY"] = b.Settings["Start"].Value.Y army["StartZ"] = b.Settings["Start"].Value.Z army["EndX"] = b.Settings["End"].Value.X army["EndY"] = b.Settings["End"].Value.Y army["EndZ"] = b.Settings["End"].Value.Z army["StartTime"] = b.Settings["StartTime"].Value end
Note: you can also do army.ID =
(same for all string keys).
Well, first of all I've forgot to add my SetAsync function into the code. Sorry about that. Secondly, I've fixed it. I am not deleting this question because I spent over seven hours on this problem and do not want anyone else to spend another seven hours on this type of problem. Just one basic sentence: You can't save tables inside tables.