Data Store 2 Error

Hi! Me and my friend are working with Data Store 2 and we’ve come across an error. Basically we’re trying to make areas save with the player and when they join back the values should be in a folder.

Here is the code ex:

		local DataStore = DataStore2(dataStoreName, Player)
		local Data = DataStore:Get()
		if Data == nil then
			DataStore:Set({
				Areas = {
					Desert = false,
					Candy = false,
					Lava = false
				},
				Leaderstats = {
					["💰 Coins"] = 0,
					["🍲 Soups"] = 0,
					["⏰ Time"] = 0
				},
				Stats = {
					MaxIngredients = 50,
					Ingredients = 0,
					IsInfinite = false,
					IngredientMultiplier = 1,
					Time = 0,
					MaxSoups = 5,
					DailyTime = os.time(),
					DailyRewardTimer = 43200
				},
			})
			Data = DataStore:Get()
		end

Everything works with the data, but the areas table doesn’t. I have the areas name in them and when I try to loop through them, I get nil.

Code:

for StatName, Value in pairs(Data.Areas) do
	
			local StatValue = Instance.new("BoolValue")
			StatValue.Name = StatName
			StatValue.Value = Value
			StatValue.Changed:Connect(function()
				local newTable = {}
				for _, val in ipairs(lstatFolder:GetChildren()) do
					newTable[val.Name] = val.Value
				end

				local oldn = newTable

				newTable = DataStore:Get()
				newTable.Areas = oldn
				DataStore:Set(newTable)
			end)
			StatValue.Parent = Area
		end

The same code is used for the Leaderstats and the Stats, but not this. I don’t know why it says it’s nil, but if anyone can help us that would be appreciated!

I would help you with this, but, I wouldn’t recommend using DataStore 2. IT’s very outdated and most of its features are already in the roblox engine. You should use ProfileService. It’s a lot similar and much easier to use in my opinion.

Remove your post as it’s not helpful.

Well you see I would use profile service, but my game is already hard coded with ds2, so changing it would be a really huge hastle.

whats the exact error message? and can you print the entire data table and examine it?

The message says invalid argument #1 to ‘pairs’ (table expected, got nil)

I’ve tried to print it, but it says the same thing. The Leaderstats and Stats both print perfectly fine.

Print “Data” so we can see the entire table

print(Data)

it seems as if “areas” doesnt exist for some reason and this doesn’t make sense

please screen shot the entirety of the outputted table

image

For some reason areas doesn’t show up.

Ok, is this old data you have? It says you have a lot of coins, so you likely already had data and the data==nil block will not occur.

you can alternatively do:

if not Data.Areas then

Data.Areas={
Desert = false,
Candy = false,
Lava = false
}

end

and then set the new data

Hey I got it to work, but not really. Resetting data stores works temporarily, but in future updates this would not work. But your idea would seem to work, thanks!

resetting data would be an entirely different ball park

rather you wouldn’t inherently reset a datastore, you would just not load it, and make a new datastore

local DataStore = DataStore2(dataStoreName, Player)

to

local DataStore = DataStore2(NewDataStore, Player)
1 Like