I’m creating a DataStore to save several values within my game. These stats include the following:
Camos (folder, instances within this folder are StringValues)
Specializations (folder, instances within this folder are StringValues)
Kills (NumberValue)
Money (NumberValue)
This is the code that I’m currently experimenting with:
local DataToSave = {["Camos"] = {}; ["Specializations"] = {}}
for Identifier, Data in pairs(Player.Data:GetChildren()) do
if Data.Name == "Camos" then
for _, Camo in pairs(Data:GetChildren()) do
print(Camo.Name)
table.insert(DataToSave.Camos, #DataToSave.Camos + 1, Camo.Name)
end
end
if Data.Name == "Kills" then
DataToSave["Kills"] = Data.Value
end
if Data.Name == "Specializations" then
for _, Specialization in pairs(Data:GetChildren()) do
table.insert(DataToSave.Specializations, #DataToSave.Camos + 1, Specialization.Name)
end
end
if Data.Name == "Money" then
DataToSave["Money"] = Data.Value
end
end
DataStore:SetAsync(Player.UserId, DataToSave) -- this is the line where the error originates from
end)
[18:47:56.422 - Cannot convert mixed or non-array tables: keys must be strings]
At this point I’m out of ideas on what to do, I’ve tried recoding this about 2-3 times already. Any sort of help would be appreciated.
Somewhere, you are attempting to save either a userdata value (Color3, Vector3, Value instance, etc), OR you have a mixed table (some numeric indices, and some string indices).
You could probably just convert your camo table and spec tables into string dictionaries instead of number arrays, e.g you could just convert the number to a string using tostring:
-- instead of this:
for _, Camo in pairs(Data:GetChildren()) do
print(Camo.Name)
table.insert(DataToSave.Camos, #DataToSave.Camos + 1, Camo.Name
end
-- do this:
local camoCounter = 0
for _, Camo in pairs(Data:GetChildren()) do
camoCounter = camoCounter + 1
print(Camo.Name)
DataToSave.Camos[tostring(camoCounter)] = Camo.Name
end
You would then be able to access it efficiently using pairs:
for _, CamoName in pairs(DataToSave.Camos) do
-- stuff with CamoName
end
After following your instructions, output states the following:
number Instance (x4)
Any ideas if this could be causing a problem? Please note that I am not directly trying to store these instances in the DataStore, instead I am saving either their name or value.
Are any of the values Color3, Vector3, Vector2, UDim2, (etc) Instances? If you are saving Instance.Value on an instance of that type, that would be your problem.
team create wouldn’t cause discrepancies in the JSON encoding (which is what is failing in the SetAsync call). If it works fine in another place, then something in the original place is causing a problem. I don’t see any obvious issues with the code provided other than some redundancies.
This seemed to do the trick I guess. I added tonumber() on certain values just as a failsafe and the error stopped occuring. Now it looks like my module is having a hard time loading data. Thanks for the help!
Edit: I also fixed loading. Again, thanks for the help.