local DataStore = game:GetService("DataStoreService") local grandopeningkitsds = DataStore:GetDataStore("GrandOpeningKits") game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("Folder",player) stats.Name = "stats" -- creates a leaderboard local statsloaded = Instance.new("BoolValue") statsloaded.Parent = player.stats statsloaded.Name = "StatsLoaded" -- CREATES GRAND-OPENING-KITS VALUE -- local grandopeningkits = Instance.new("NumberValue") grandopeningkits.Parent = player.stats grandopeningkits.Name = "GrandOpeningKits" -- creates a leaderstat value grandopeningkits.Value = grandopeningkitsds:GetAsync(player.UserId) or 0 -- gets past value grandopeningkitsds:SetAsync(player.UserId,grandopeningkits.Value) -- sets a new value local grandopeningkitnum = grandopeningkits.Value -- SETS STATSLOADED VALUE TO TRUE -- statsloaded.Value = true -- LEAVE BACKUP SAVE -- game.Players.PlayerRemoving:connect(function(player) print("Saving Data for user "..player.UserId) grandopeningkitsds:SetAsync(player.UserId,player.stats.GrandOpeningKits.Value) print(player.UserId.."'s Data has been saved successfully.") if error then print("DATA ERROR: "..player.UserId.."'s Data has been saved unsuccessfully") end end) end)
I use a similar script on another game and it works fine. Anybody know whats going on?
Hello. I'm not sure if this will solve your problem, but here's the code I have created. (cloned and edited)
local DataStore = game:GetService("DataStoreService") local grandopeningkitsds = DataStore:GetDataStore("GrandOpeningKits") game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("Folder",player) stats.Name = "stats" -- creates a leaderboard local statsloaded = Instance.new("BoolValue") statsloaded.Parent = player.stats statsloaded.Name = "StatsLoaded" statsloaded.Value = false -- CREATES GRAND-OPENING-KITS VALUE -- local grandopeningkits = Instance.new("NumberValue") repeat wait() print('Waiting for stats folder..') until player.stats grandopeningkits.Parent = player.stats grandopeningkits.Name = "GrandOpeningKits" -- creates a leaderstat value local amt = grandopeningkitsds:GetAsync(player.UserId) if amt == nil then amt = 0 end grandopeningkits.Value = amt -- SETS STATSLOADED VALUE TO TRUE -- statsloaded.Value = true -- LEAVE BACKUP SAVE -- Bad idea. It will cause Datastore limit errors to break the script. end) game.Players.PlayerRemoving:connect(function(player) print("Saving Data for user "..player.UserId) if player.stats.StatsLoaded.Value == true then local success, err = ypcall(function() grandopeningkitsds:SetAsync(player.UserId,player.stats.GrandOpeningKits.Value) end) if success == true then print(player.UserId.."'s Data has been saved successfully.") else print(player.UserId.."'s Data has been saved unsuccessfully.") end else print(player.UserId.."'s Data has been saved unsuccessfully. Had not loaded.") end end)