If i replace uid_ with anything else it doesnt work anymore
local DSS = game:GetService("DataStoreService") local datastore = DSS:GetDataStore("GeneralSaveData", "Players") function generateDataKey(player) local ret = ("uid_" .. player.userId) return ret end function generateDataTable(player) local dataTable = { Units = player.leaderstats.Units.Value, Level = player.leaderstats.Level.Value, Xp = player.leaderstats.Xp.Value } return dataTable end function saveDataForPlayer(player) local key = generateDataKey(player) local data = generateDataTable(player) datastore:SetAsync(key, data) end function inputDataToPlayer(player, data) player.leaderstats.Units.Value = data.Units player.leaderstats.Level.Value = data.Level player.leaderstats.Xp.Value = data.Xp end function loadDataForPlayer(player) local key = generateDataKey(player) local data = datastore:GetAsync(key) inputDataToPlayer(player, data) end game.Players.PlayerAdded:connect(function(player) player:WaitForChild("leaderstats") loadDataForPlayer(player) --Load first thing when they join end) game.Players.PlayerRemoving:connect(saveDataForPlayer) --Save data when the player leaves the game
Because when you change the key that you saved the data with, you will never be able to access that data again unless the key is the same. Just like the key to a house. If I were to go buy a random key to replace my current one and then expect to be able to unlock my house I would be stupid. What you are doing is similar to getting a random key. You are making it impossible to load the data from the same "house" ever again unless you change the key back. When you save data with a "key" you are setting the locks of the "house" to only open to that key. So if you change the key it will not let you into that "house" again, but it will create a new "house" for you to store data in.