I can not find any solution so far. I already looked for more solutions.
Block of code that is giving trouble:
(top is data that is going to be saved, bottom is data printed from the datastore)
I lost [“owned”] value for some reason.
This is the mixed array issue. If you put numerical indices (starting from 1) and keys in a table, the way it gets handled on the background changes. You can just convert numerical indices to strings and see all keys stored.
Example:
local data = {
[1] = "A",
["O"] = "B",
}
local newTab = {}
for i, val in data do
local key = i
if type(i) == "number" then
key = tostring(i)
end
newTab[key] = val
end
print(newTab) -- { ["1"] = "A", ["0"] = "B" }
local data = {
[1] = "A",
["O"] = "B",
}
local newTab = {}
for i, val in data do -- Shouldnt this be for i, val in pairs(data) do?
local key = i
if type(i) == "number" then
key = tostring(i)
end
newTab[key] = val
end
print(newTab) -- { ["1"] = "A", ["0"] = "B" }
When you pass tables across Roblox’s internal implementations (such as the one you are having issue with, datastores, and RemoteEvents) the tables go through a process that rearranges the table. If the entire table is an array, it’s ordered in integers starting from 1 (indices start from 1 in Lua). You can also have dictionaries with keys, therefore strings. If you mix up both numerical and string indices, it becomes a mixed array. Roblox handles it so that keys get removed if you put numerical indices (should start from 1 again).
You could also just convert the table to JSON and then compress that string, killing two birds with one stone. Once you retrieve the data, decompress it then JSONDecode it. This also saves you the trouble of converting indices to strings. (Also if you have a string variant of a numerical index already in the table for some reason e.g. “1” & 1)
Would making a second datastore that just stores strings work as well? I know it probably is not the best solution, but it will be a easy fix and would require less code than other solutions.