DataStore not loading

So the problem I’m having is that my DataStore is not loading any data. It’s giving me an error that says:

ServerScriptService.*script*:188: invalid argument #3 (string expected, got nil) 
-- === DataStorageStats[i].Value = DataStore:GetAsync(DataStorageStats[i].Name) as you see later.

My datastore script looks like this:

game.Players.PlayerAdded:Connect(function(Player)		
	local DataStore = DataStoreService:GetDataStore(Player.UserId .. "Data")
	local DataStorageStats = Player.Data.Stats:GetChildren()
	local DataStorageSettings = Player.Data.Settings:GetChildren()
	
	if Player:WaitForChild("Data") ~= nil then
		local success, result = pcall(function()
			for i = 1, #DataStorageStats do
				DataStorageStats[i].Value = DataStore:GetAsync(DataStorageStats[i].Name)
			end
			for i = 1, #DataStorageSettings do
				DataStorageSettings[i].Value = DataStore:GetAsync(DataStorageSettings[i].Name)
			end
		end)
		
		if success then
			print("Loaded!")
			Player.Data.Loaded.Value = true
		else
			print("No!!!!")
			warn(result)
		end
	end
end)

What I want my script to do is save the data from the settings and the stats.

One thing I think could be the problem is that DataStorageStats[i].Value is not true/is not equal to anything, obviously I did add data to them as they are folders. The other thing is that it is trying to call it with the for loop for too long???

1 Like

Is your saving script correctly saving it?

2 Likes

Yes it is. I changed a value in my data on my player and it works now, I’ll do a couple more tests for stuff to know for sure.

1 Like

Oh okay. Though the way your data is being handled is a bit odd, you might want to change it

2 Likes

I know and I DEFINITLY know that there will be errors with it but until then I’ll keep it how it is.

1 Like

My guess is since one of the stats is a string value you set it to nil and it errors since a string value only takes a string. just add an if statement to check if one of them is a string value and instead replace the value with “” or something.

2 Likes

I’ll keep that in mind if it happens again for some random reason.

1 Like

Ok so update. I have to set data for the player every time there’s no data but I want it to automatically load the data, even if the player has no money, settings I just want it to load blankly because if it doesn’t, my whole game stats and settings system won’t make any sense.

1 Like