Leaderstats not saving

Hey there,
I have this leaderstats script that’s been working fine but just recently it stopped saving. I’ve tried finding the problem through the print function but they worked but the leaderstats still did not save. Anybody see the problem?

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SkinStats")
game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local Cash= Instance.new("IntValue", Leaderstats)
	Cash.Name = "Cash" 
	Cash.Value = 0
	local Kills= Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills" 
	Kills.Value = 0

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Cash.Value = Data.Cash 
		Kills.Value = Data.Kills 
		print("also works")
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Cash"] = Player.leaderstats.Cash.Value; 
		["Kills"] = Player.leaderstats.Kills.Value; 
	})
end)
print("works")
1 Like

Have you tried doing this in an actual server? Sometimes when you end a test session in Roblox studio, things like DataStores don’t have time to run.

1 Like

The saving system is fine, but remember that you are saving the data in a table and the data is a dictionary.

You should put the data in values ​​like this:

Cash.Value = Data["Cash"]
Kills.Value = Data["Kills"]

did you ever look into a scenerio where the game closes?

3 Likes

won’t change anything, but it visually looks better that way, you could also use that way using a for loop if you wanted to

2 Likes

Following with this suggestion, you could bind a wait function to run when the game closes so that the server has time to handle the datastore (and possibly other things)

1 Like

I mean BindToClose has a full 30 seconds to run, it is enough time to save my data
also I have another suggestion

you should use UpdateAsync instead

EDIT: you might find this link interesting Stop using SetAsync() to save player data

3 Likes

Yep, same thing happened I also have api turned on

1 Like

Okay. @D0RYU already said it, but try using the BindToClose event (in any script) in order to allow the server more time to run scripts once the game is closed.

Edit: Ask me if you need me to elaborate on what you should put in the function

1 Like
game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local Cash= Instance.new("IntValue", Leaderstats)
	Cash.Name = "Cash" 

	local Kills= Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills" 

	local Data
    local got,Newplayer = pcall(function()
       Data = DataStore:GetAsync(Player.UserId)
    end)
    if got and Data then
      Cash.Value = Data.Cash
      Kills.Value = Data.Kills
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
   local Success,Error = pcall(function()
    DataStore:SetAsync(Player.UserId, {
		Cash = Player.leaderstats.Cash.Value,
		Kills = Player.leaderstats.Kills.Value 
	})
   end)
   if not Success then warn(Error) end
end)

game:BindToClose(function()
   if game:GetService("RunService"):IsStudio() then
      wait(1)
   else 
      for _, v in pairs(game:GetService("Players"):GetPlayers()) do
         local Success,Error = pcall(function()
               DataStore:SetAsync(v.UserId,{
                 Kills = v.leaderstats.Kills.Value,
                 Cash = v.leaderstats.Cash.Value
               })
         end)
         if not Success then
              warn(Error)
         end
      end
   end
end)

This is the Fixed script.
Also, Can you show Where you Define dataStore?

1 Like

Ill try it myself and if I have any quires ill be sure to get back to you, Thanks!

this should be

for _, v in pairs(game:GetService("Players"):GetPlayers()) do

also you should really add spaces after using commas(EXAMPLE: Success, Error is better then Success,Error)

1 Like

Side question for you. What difference does the spaces between commas do for a script? I’m assuming it is just for better looks and organization, but it doesn’t have any effect does it?

it doesn’t change the functionality of the script at all, but it is good practice to add spaces because readability is much better.
also as you work with them more and more you start to understand why spaces are so helpful when reading your code or someone else’s code

Hey, I fixed it but in a different way, I had a script that was identical to that script but with different a datastore name and not called leaderstats to save a different value and once I deleted that it worked.

Oh :grin: It’s always annoying when you think you know what the issue is, but it ends up being something completely different. Glad you figured it out!

1 Like

Thank you, but just on that matter would you mind helping me save a boolvalue to a player?

Sorry, I didn’t see this in my notifications. Saving a bool is the same method as saving a number, or a string. There is nothing you need to do differently, other than defining whether it is true or false.