When indexing table in Profile Store it returns nil

Hi everyone,

I’m experiencing an odd issue with data stores using profile service. So this is the template for my data

local Data = {}
Data.DEFAULT= {
Skips = {
	[5] = 0,
	[10] =0,
	[20] =0,
	[40] = 0,
	[60] = 0,
	[120] =0,
	[240] = 0,
	[300] = 0,
   }
},
return Data

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.

1 Like

Hi Kizylle,

Thank you for the suggestion. so I tried setting up array without any gaps

	Skips = {
		[1] = 0,
		[2] =0,
		[3] =0,
		[4] = 0,
		[5] = 0,
		[6] =0,
		[7] = 0,
		[8] = 0,
	},

but printing the item still returns nil

print(profile.Data.Skips[5]) -- Still returning nil instead of 0

Definitely not the issue as I have tested gapped indexes, and they continue to work as expected.

The easy fix for this should be printing profile.Data to see what’s actually inside, and then you could use that to guide your debug process.

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)

Hi HeCatchThose,

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.
Screenshot 2025-05-06 170114

As you can see, you would expect

print(profile.Data.Skips[5]) -- Still returning nil instead of 0

to return 0, but instead it returns nil

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.

1 Like

Hi everyone,

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:

print(table1["5"])

Hello Crepz,

Thank you for your solution, but it happened that I posted mine as you posted yours!

1 Like

No worries, I’m glad you got it!

1 Like