Tips for creating a data saving system

I’m re-coding one of my games entirely and was looking to make everything more efficient. One of the things I want to improve is the data storing.

How I did it a few years ago was I had folders filled with values (String values, int values, etc) in the player. Upon entering I would give all of these values and save these to an array in a datastore upon leaving.

As for backups, I also had a separate datastore where each time a player leaved, I would include a string of all the values separated by commas and add it to an array of other previous saves and save that to a backup datastore until the string limit was reached at which point it would reset the backup.

Basically it was very inefficient. I am looking for tips on how to create an easy to manage data system, with reliable backups. I’ve seen a backup system where you make a separate data store every single time a user plays, but that would take a lot of time to delete upon receiving a GDPR request.

Are there any tips or tricks I could use to improve my old system?

Try this

https://devforum.roblox.com/t/how-i-implemented-datastore2-into-my-game-and-how-you-could-too/324119/3

I used DataStore2 in my game, and this post explains how I did it and how you can too, or you can watch AlvinBlox’s video on how to use DataStore2 (very insightful). This is probably one of the best ways to save data out there. (It can even use backups, more on that here on the original post.

AlvinBlox’s Video

2 Likes

Something I do which has worked out great is creating an empty table which holds all data for each player within a server.

local STATS_DATA = {}

In a module script, I will then create different functions to update, add, and save this data. To add the data to the table when the Player joins I do this(Note I use Datastore2 which @Theevilem mentioned, you can always create your own data handler but Datastore2 just makes it so much easier)

	game.Players.PlayerAdded:Connect(function(playerJoined)
	local playerStatsData = DataStore2("PlayerStatsDataV12", playerJoined)
	
	local success, data = pcall(function()
		return playerStatsData:Get()
	end)
	
	if success then --Get Player Stats Data
		if data then
			CURRENT_SESSION[playerJoined] = data
			loadPlayerStats(playerJoined)
		else
			--New player has joined
			CURRENT_SESSION[playerJoined] = DEFAULT_STATS_TABLE
			playerStatsData:Set(CURRENT_SESSION[playerJoined])
			loadPlayerStats(playerJoined)
		end
	else
		print(data.." ERROR")
	end
end)

You can then update this data just by adding a function that goes through the table, finds the Player’s data and changes said stat.

function dataLib:UpdateStat(player, STATNAME, NEWVAL)
	assert(typeof(CURRENT_SESSION[player][STATNAME]) == typeof(NEWVAL), "ERROR: VALUE IS NOT THE SAME AS STAT")
	if typeof(CURRENT_SESSION[player][STATNAME]) == "table" then
		--Inventory
	else
		CURRENT_SESSION[player][STATNAME] = NEWVAL
	end
end

This is what I am currently using and it works great. This is also all done in a module script so I will always have access to the Data within server scripts if I ever need to update it.

5 Likes

I love how you explained that, and yes, DataStore2 has to be one of the most helpful Development resources available :smiley:

2 Likes

Heres a good tutorial on how to save using DataStore2 with tables/dictonaries allowing for more ease in development:

4 Likes