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 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.
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
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.
Ok, a few things because I don’t want to restate what others have said but you did say you were a beginner
Use playerid, not player name as if the player changes their name they will lose their data
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.
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.
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
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