How to change the way you use data stores without deleting userdata

I currently updated my games datastore script from using multiple keys for each leaderstat to combining them all on a dictionary and saving that to a single key, it’s much more efficient however, the problem is that all the players that played it before lost their data, my game doesn’t usually get players yet and most of those accounts were test accounts, but I would like to know if there is a way prevent data loss when updating a datastore script

2 Likes

An easy way to port data to a new system is when a player joins try to load data from the new datastore, if it doesn’t exist then attempt to load it from the old datastore. If that fails then they are a new player, so create a default set of data for them.

And for all players only save data to the new datastore.

3 Likes

source found from Roblox studio help center.

Before we dive into data storage, let’s set up the system which keeps track of a player’s money and experience during a game session. We’ll start by creating a ModuleScript , a special type of script that can be referenced from other scripts. An ideal place for this ModuleScript is within ServerStorage .

local PlayerStatManager = {}
 
-- Table to hold player information for the current session
local sessionData = {}
 
local AUTOSAVE_INTERVAL = 60
 
-- Function that other scripts can call to change a player's stats
function PlayerStatManager:ChangeStat(player, statName, value)
	local playerUserId = "Player_" .. player.UserId
	assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
	if typeof(sessionData[playerUserId][statName]) == "number" then
		sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
	else
		sessionData[playerUserId][statName] = value
	end
end
 
-- Function to add player to the "sessionData" table
local function setupPlayerData(player)
	local playerUserId = "Player_" .. player.UserId
	sessionData[playerUserId] = {Money=0, Experience=0}
end
 
-- Connect "setupPlayerData()" function to "PlayerAdded" event
game.Players.PlayerAdded:Connect(setupPlayerData)
 
return PlayerStatManager```

I don’t see how this would help me as if I change my data store script to use different keys or data stores, all userdata will reset, even data in running servers as the scripts won’t update to use the new one