I'm including a lot of player stats in my game, which must be saved to Data Stores. I was wondering if the best way to do that would be to create a table of all the player stats and save only one thing, or save all the values individually and load them individually. My code right now consists of 2 stats, "Level" and "GamesPlayed". This is what I have so far:

Memory = game:GetService("DataStoreService"):GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(plr)

	local Level = Instance.new("IntValue",plr)
	Level.Name = "Level"
	
	local GamesPlayed = Instance.new("IntValue",plr)
	GamesPlayed.Name = "GamesPlayed"
	
	local SavedGamesDataFetchSuccess, SavedGamesErrorMessage = pcall(function()
		SavedGames = Memory:GetAsync(plr.UserId.."-Games")
	end)
	
	if SavedGamesDataFetchSuccess then
		if SavedGames ~= nil then
			print("DataLoaded")
			GamesPlayed.Value = SavedGames
		else
			print("NewPlayer")
			GamesPlayed.Value = 0
		end
	else
		plr:Kick("There was a problem loading data, please try again later.")
	end
	
	local LevelDataFetchSuccess, LevelErrorMessage = pcall(function()
		SavedLevel = Memory:GetAsync(plr.UserId.."-Level")
	end)
	
	if LevelDataFetchSuccess then
		if SavedLevel ~= nil then
			print("DataLoaded")
			Level.Value = SavedLevel
		else
			Level.Value = 1
		end
	else
		plr:Kick("There was a problem loading data, please try again later.")
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local id = player.UserId
	
	local GamesPlayed = player:WaitForChild("GamesPlayed")
	local GamesPlayedValue = GamesPlayed.Value
	
	local Level = player:WaitForChild("Level")
	local LevelValue = Level.Value
	
	local GamesPlayedDataWriteSuccess, GamesPlayedErrorMessage = pcall(function()
		Memory:SetAsync(id.."-Games", GamesPlayedValue)
	end)
	
	local LevelDataWriteSuccess, LevelErrorMessage = pcall(function()
		Memory:SetAsync(id.."-Level", LevelValue)
	end)
	
	if not LevelDataWriteSuccess then
		
		local RetryCount = 0
		
		while RetryCount <= 6 do
			wait(60)
			local Succeeded, Error = pcall(function()
				Memory:SetAsync(id.."-Level", LevelValue)
			end)
			if Succeeded then
				break
			end
			RetryCount = RetryCount + 1
		end
	end
	
	if not GamesPlayedDataWriteSuccess then
		
		local RetryCount = 0
		
		while RetryCount <= 6 do
			wait(60)
			local Succeeded, Error = pcall(function()
				Memory:SetAsync(id.."-Games", GamesPlayedValue)
			end)
			if Succeeded then
				break
			end
			RetryCount = RetryCount + 1
		end
	end
	
end)

Right now I am saving levels and games played individually and checking if the save ran correctly individually. Should I go along with this when I create the rest of the stats, or should I create one table of all the stats together? I don't know much about data stores, so I'm not sure which one would be better. In case it matters, I'm planning on adding about 18 more stats, so I'll have a lot. Thank you for reading!

2 comments

0

Usually people use tables however if you want to use something more safer you can use DataStore2, an open-source module script. It has backups in case data fails and stuff. Many games actually use this module such as Dungeon Quest and Fairy World.

0

I use dictionaries