I am currently trying to work on a datastore system which will be able to save all data from the leaderboards without defining the stat (ex. gold.Value = data). The reason I am trying to do this is because I have a lot of stats to save in a lot of different places, and manually writing them all would be tedious and difficult to edit, seeing as the code comes out at 500+ lines. The issue is that with the current code it will only save the Player.Name and not the Player.UserId. Player.Name could result in loss of data if a player were to change their username. That is certainly not acceptable considering that my game is based around leveling up. The code is below.
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("Saves Data: "..i)
end
print("Stats Have Been Saved")
end)
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 "..i.." is identified")
end
end)
I have tried to make the storage defined differently, although to no appeal. I understand that Player.UserId is not a member of “Stats”, however I do not understand why the Player.Name is allowed. Essentially, I have a script which currently works at saving the player’s data by their name, and not their userid, which is far more im,portant. However, while I know what needs to be accomplished, I do not know how to do it. If any help can be offered, that would be greatly appreciated!
NOTE: This is my first DevForum post, so I apologize if I am not doing it correctly.