Basically, I’m storing a table in the data store. The problem is that whenever I try to call a value in this table, it returns nil. For example,
print(profile.Data.Skips[5]) --prints nil, when should print 0
Now the interesting thing is when I set the playerstore to mock
if game:GetService("RunService"):IsStudio() then
PlayerStore = PlayerStore.Mock
end
It works perfectly fine, and prints the value it is supposed to. This is quite interesting, as the code works only if I’m not saving data. I would really appreciate any help and let me know if you need more details, like how I load and setup the profiles
Arrays can’t have gaps if you’re transmitting data, either via remote or to one of roblox’s services. An
array like this: {[1] = true, [3] = true} reads as {[1] = true} as far as it’s concerned. You shouldn’t have gaps unless you know what you’re doing since an array with a gap can have other unintended side effects. Like doing # on {[1] = true, [3] = true, [4] = true} would only return 1 even though there are 3 members in the table.
If you really want to use numbers, turn them into strings instead. Then you’d be saving a dictionary instead of an array.
You have tested in which context? Doing {[1] = true, [5] = true} then indexing 5 works, it starts to fall apart when back end code tries iterating over the table or converting it into json data (which iirc is what datastores do)
I appreciate your contribution but I have already done that. When I print profile.Data, it returns all the values as expected. I even printed the Skips table and it returned all the values expected.
As you can see, you would expect
print(profile.Data.Skips[5]) -- Still returning nil instead of 0
The issue isn’t with your data being missing, it’s just how you’re indexing it. All your keys in the Skips table are strings like “5”, “10”, etc., but when you do profile.Data.Skips[5], you’re using a number, not a string. Lua treats “5” and 5 as two totally separate keys, so it returns nil.
Just do profile.Data.Skips[tostring(5)] and it’ll return 0 as expected.
I have figured out my problem. The reason why it was returning nil was because of the gaps in the “array,” just like what @Kizylle said. Correct me if I’m wrong, but it looks like that if you use profile store (or maybe datastore in general) the arrays with gaps turn into dictionaries, and the index become strings instead of numbers.
Example:
-- In table1, it looks like the indexes are numbers
Table1 = {
[2] = 0,
[5] = 0,
[8] = 0,
}
--But when you print the table, it looks like this (while using profile store)
Table1 = {
["2"] = 0,
["5"] = 0,
["8"] = 0,
}
to solve this issue, I removed the gaps but you could also possible index a value like: