How to change datastore script without deleting the users data

I’ve changed the “player.Name” to “player.UserId” But it seems that the users data is gone, how can I Fix that?

**Not My Script

--[[Savin'
		Dem
			Stats	
--]]
game.Players.PlayerRemoving:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.UserId.."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.UserId.."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)

You want to copy the data from player.Name to player.UserId?

Yes, I Don’t want the players data to reset when I Replace the script from “player.Name” to “player.UserId”

You need to check if the player has any data stored in player.Name and copy it over to player.UserId if that DataStore is still empty of course. You can use DataStore:GetAsync() for this, it returns nil if the DataStore has no data.

You can use GetUserIdFromNameAsync() on each name in the table

How do I Check it? I Barely Know to script but it’s really important to me

I think you might have to check if they have data in the old format when they join the game, and if they do set the value from the old one to the new one, then set the old values to nil. I would type out the script but I’m on my phone

Something like:

local DSS = game:GetService('DataStoreService')
local DS1 = DSS:GetDataStore(player.UserId)
local statstorage = player:FindFirstChild("leaderstats"):GetChildren()

local DS2 = DSS:GetDataStore(player.Name..'Stats')

if not DS1:GetAsync(player.Name..'Stats') then
	for index, Value in ipairs(statstorage) do
	DS1:SetAsync(Value.Name, DS2:GetAsync(Value.Name))
	end
	for index, Value in ipairs(statstorage) do
	DS2:SetAsync(Value.Name, false)
	end
end

I’m not sure, but I don’t think you can set a ‘‘normal’’ DataStore value to nil. That’s why I used false here.

If you don’t change the datastore name, scope, or key for each player, you will not erase user data.

1 Like

I don’t think you can erase all data from a DataStore.

I’m sorry if I Sound stupid, but where do I put this script?

Ok, a few things because I don’t want to restate what others have said but you did say you were a beginner

  1. Use playerid, not player name as if the player changes their name they will lose their data
  2. As @janpieterkes said, you need to include a line that copies the players data over IF they had data from their username, or a “redundacy line”. This would look something like this:
local success, erorrMessage = pcall(function() --Wrap in a pcall incase error (no data associated with the username)
    local olddata = Datastore:GetAsync(player.Name) --Get old data, replace the Datastore with your name
    Datastore:SetAsync(player.UserId) --Set the old data to the players new table
end)
if not success then 
    warn(errorMessage) --Will print the error to console, if you dont want this just do task.wait()
end

This should fix it, but I definitley reccomend learning a bit more in terms of basic coding before attempting an advanced topic like datastores. Keep in mind that you will still have to tell the Datastore what to save and load.

If it were to go there, how many peoples data would be lost? Just wondering

You can erase all Data from the DataStores, all that would be left however are the Keys.

around 2600-2700 players have data

Hm, okay. Have you tried doing it manually with a plugin called BloxBiz?

Also, try this and tell me if it works

Also, why did you use Player.Name, The Players Name is not permanent. And the Players UserId is, if you have a Key for a Player, that is under their name. And they decide to change their bame, the Data would be lost because you are trying to Access another Key.

1 Like

Where do I Put this script,

in the Data script, ServerScriptService or something else?

DataToTransfer = DataStore1:GetAsync(Key) --if you are saving by UserId, Key would be the user you want to work with's UserId

DataStore2 = DataStore2:SetAsync(Key, DataToTransfer) --Set the value of the second DataStore to the value of the first
1 Like

You would put this in the datastore script. And change first Key to the players name, second key to players userid. Wrap it in a pcall function like I did before