Changing Data without resetting the data

How do I change the data from Player.Name to Player.UserId without resetting the player’s data?

--[[Savin'
		Dem
			Stats	
--]]
game.Players.PlayerRemoving:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

	local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
	for i =  1, #statstorage do
		datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
		print("saved data number "..i)

	end
	print("Stats successfully saved")	
end)


--[[
	Loadin'
		Dem
			Stats
--]]
game.Players.PlayerAdded:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

	player:WaitForChild("leaderstats")
	wait(1)
	local stats = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #stats do			
		stats[i].Value = datastore:GetAsync(stats[i].Name)
		print("stat numba "..i.." has been found")
	end
end)

In PlayerRemoving, just change Name to UserId.

In PlayerAdded, define a variable datastore2 that is the same as datastore, but has the player user ID instead of the name.
Then, replace

for i = 1, #stats do			
		stats[i].Value = datastore:GetAsync(stats[i].Name)
		print("stat numba "..i.." has been found")
	end

with

for i = 1, #stats do		
        local Loaded = datastore:GetAsync(stats[i].Name)
        if Loaded then
            datastore:RemoveAsync(stats[i].Name)
             datastore2:SetAsync(stats[i].Name, Loaded)
        else
            Loaded datastore2:GetAsync(stats[i]).Name
        end	
		stats[i].Value = 
		print("stat numba "..i.." has been found")
	end

Note that this will effectively double the datastore calls, however its unavoidable.

Also, you should consider storing a table for all the data, instead of having a datastore entry per statistic. You are likely to encounter rate limits with the current setup.

1 Like

Hey, first of all, I recommend you take a closer look at the DataStoreService documentation and also a deep tutorial into DataStores (by Roblox itself).

Making new DataStores (the :GetDataStore function requires an API Request on the roblox servers to get your DataStore) for each player is considerably slow. The right and best way to do a DataStore system is to use a single DataStore with specific keys for each player (such as UserId) that you can use to access the data in the DataStore.

See this example below:

local DataStoreService = game:GetService("DataStoreService")
local ExampleDataStore = DataStoreService:GetDataStore("Example")

local Players = game:GetService("Players")

Players.PlayerAdded:connect(function(Player)
	local PlayerData, Error = pcall(function() 
		return ExampleDataStore:GetAsync(Player.UserId) or "[DefaultData]"
	end)
	-- GetAsync is a function that involves roblox's servers, so you should always use pcall, as these may be down or slow at the moment
	-- GetAsync can sometimes return nil, if the player is logging in for the first time, for example, he doesn't have any saved progress yet
	
	-- Good for debugging, if GetAsync returns nil it will print the possible error. 
	if not PlayerData then
		error(Error)
	end
	
	-- Do stuff with player's data
end)

Players.PlayerRemoving:Connect(function(Player)
	-- Get the updated player's data, for a "Money" DataStore for example, you should get the Money value the player had when left the game.
	local Value = "[UpdatedValue]"
	
	local Success, Error = pcall(function()
		return ExampleDataStore:SetAsync(Player.UserId, Value)
	end)
	
	if not Success then
		error(Error)
	end
end)

This is the best way to make a simple DataStore system. Read the two documents I’ve given you and give them a try.

When you feel ready to make a more complicated DataStore system, I recommend using ProfileService.

I’ve tried to help you in a way that you can learn, as DataStore systems are always specific to each person, so the best way to get to grips with DataStores is to learn how to operate them and make a good, efficient system.

I wish you all luck! :hammer_and_pick:

1 Like