local Stats = {"Stat1","Stat2","Stat3","Stat4","Stat5" ,"Stat6","Stat7","Stat8","Stat9","Stat10"} local function Create(User) local StatsFolder = Instance.new("Folder",User) StatsFolder.Name = "leaderstats" for i = 1,#Stats do Instance.new("IntValue",StatsFolder).Name = Stats[i] end end game.Players.PlayerAdded:connect(function(Player) Create(Player) end)
This script creates the stats
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerStats") game.Players.PlayerRemoving:connect(function(Player) Player:WaitForDataReady() local Stats = Player:FindFirstChild("leaderstats"):GetChildren() for i = 1, #Stats do DataStore:SetAsync(Stats[i].Name,Stats[i].Value) end end) game.Players.PlayerAdded:connect(function(Player) Player:WaitForDataReady() local Stats2 = Player:FindFirstChild("leaderstats"):GetChildren() for i = 1, #Stats2 do Stats2[i].Value = DataStore:GetAsync(Stats2[i].Name) end end)
This script saves the stats
I don't want my players to work hard and then lose all their data when they leave, is this system likely to fail?
Your script currently will not work as you intend it due to the keys used. In addition to this you do not manage any of the potential errors with a data store (see limitations).
Side note, you do not use Player:WaitForDataReady()
as this is for Data persistence not a data store. A data store does not require a player to store any data.
They keys used are fixed meaning that the data will be shared for all players, this is a simple fix as it only requires you to make the key unique for each player by adding in their UserId to the key.
You should also check the wiki on the limitations of a data store