DataStore problems with tables

Well, the following script is to configure the game and save its data, however, it gave me errors so I decided to make everything be saved in a single table. When making it save in a table, it gave an error because the name of the entry to load that data from the data that was saved, does not exist so the script tries to look for it but since it does not exist, it does not load and gives an error (when you make it exist no longer gives an error). If the name of the entry does not yet exist, that it does not error and that it assign a default one

local DataStoreService = game:GetService("DataStoreService")

local MyDataConfiguration = DataStoreService:GetDataStore("ConfigurationDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local DataFolder = Instance.new("Configuration")
	DataFolder.Name = "SettingsData"
	DataFolder.Parent = player
	local GraphicsFolder = Instance.new("Folder")
	GraphicsFolder.Name = "Graphics"
	GraphicsFolder.Parent = DataFolder
	local GameFolder = Instance.new("Folder")
	GameFolder.Name = "GameSettings"
	GameFolder.Parent = DataFolder
	local KeysFolder = Instance.new("Folder")
	KeysFolder.Name = "Keys"
	KeysFolder.Parent = DataFolder

	local gameSettingsBools = {
		{type = "StringValue", name = "FPS"},
		{type = "StringValue", name = "Ping"},
		{type = "StringValue", name = "BrilloVol"},
		{type = "StringValue", name = "PlayerChose"},
		{type = "StringValue", name = "Language"},
	}

	for _, value in pairs(gameSettingsBools) do
		local val = Instance.new(value.type)
		val.Name = value.name
		val.Parent = GameFolder
	end

	local GameData = nil

	local success, err = pcall(function()
		GameData = MyDataConfiguration:GetAsync(player.UserId)
	end)
	if success then
		if GameData ~= nil then
			if GameData.FPS ~= nil then
				GameFolder:WaitForChild("FPS").Value = GameData.FPS
			else
				GameFolder:WaitForChild("FPS").Value = "UnEnabled"
			end
			if GameData.Ping ~= nil then
				GameFolder:WaitForChild("Ping").Value = GameData.Ping
			else
				GameFolder:WaitForChild("Ping").Value = "UnEnabledPing"
			end
			if GameData.Gamma  ~= nil then
				GameFolder:WaitForChild("BrilloVol").Value = GameData.Gamma 
			else
				GameFolder:WaitForChild("BrilloVol").Value = "BrilloVolumen5"
			end
			if GameData.Friends ~= nil then
				GameFolder:WaitForChild("PlayerChose").Value = GameData.Friends
			else
				GameFolder:WaitForChild("PlayerChose").Value = "ForLocalPlayer"
			end
			if GameData.Language ~= nil then
				GameFolder:WaitForChild("Language").Value = GameData.Language
			else
				GameFolder:WaitForChild("Language").Value = "English"
			end
		else

			if not GameData.FPS then
				GameFolder:WaitForChild("FPS").Value = "UnEnabled"
			end
			if not GameData.Ping then
				GameFolder:WaitForChild("Ping").Value = "UnEnabledPing"
			end
			if not GameData.Gamma then
				GameFolder:WaitForChild("BrilloVol").Value = "BrilloVolumen5"
			end
			if not GameData.Friends then
				GameFolder:WaitForChild("PlayerChose").Value = "ForLocalPlayer"
			end
			if not GameData.Language then
				GameFolder:WaitForChild("Language").Value = "English"
			end
		end
	else
		print("hubo un error al guardar / there was an error saving")
		warn(err)
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
		local ConfigurationData = {
			FPS = player.SettingsData.GameSettings.FPS.Value;
			Ping = player.SettingsData.Graphics.Ping.Value;
			Gamma = player.SettingsData.Graphics.BrilloVol.Value;
			Friends = player.SettingsData.Graphics.PlayerChose.Value;
			Language = player.SettingsData.Graphics.Language.Value;
	}
	local Success, err = pcall(function()
		MyDataConfiguration:SetAsync(player.UserId, ConfigurationData)
	end)

	if (Success) then
		print("Se ha guardado correctamente / Has been saved successfully")
	else
		print("hubo un error al guardar / there was an error saving")
		warn(err)
	end
end)

It appears there are two issues.

Firstly, when you’re handling what to do if there is no data loaded, you reference GameData.FPS, etc. as if they exist, which you know they already don’t (GameData is nil). Thus, it should just be:

else
	GameFolder:WaitForChild("FPS").Value = "UnEnabled"
	GameFolder:WaitForChild("Ping").Value = "UnEnabledPing"
	GameFolder:WaitForChild("BrilloVol").Value = "BrilloVolumen5"
	GameFolder:WaitForChild("PlayerChose").Value = "ForLocalPlayer"
	GameFolder:WaitForChild("Language").Value = "English"
end

Secondly, when you’re saving, you’ve put .Graphics instead of .GameSettings in the ConfigurationData table.

Your code functioned after these issues were fixed, from my inspection.

1 Like

Well, I also thought about that but I thought it was not very professional