My leaderboard stats will not save

  1. What do you want to achieve? Keep it simple and clear!
    Leaderstats that save.
  2. What is the issue? Include screenshots / videos if possible!
    Leaderstats do not save
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I made another post but its been a very long time and not a single person answered with anything that could help me in any way.
local ds = game:GetService("DataStoreService"):GetDataStore("SaveName")

game.Players.PlayerAdded:Connect(function(plr)
	wait()

	local key = "user_"..plr.UserId

	local savevalue1 = plr.leaderstats.Rebirths

	local GetSaved = ds:GetAsync(key)
	if GetSaved then
		savevalue1.Value = GetSaved[1]
		
	else
		local valuesForSaving = {savevalue1.value}
		ds:GetAsync(key, valuesForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local savevalue1 = plr.leaderstats.Rebirths
ds:SetAsync("user_"..plr.UserId, {savevalue1.Value})
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetChildren()) do
		plr:Kick()
	end
end)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”) --making leaderstats
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local Size = Instance.new("IntValue") --making coins
Size.Name = "Size"
Size.Parent = leaderstats

local Rebirth = Instance.new("IntValue") --making coins
Rebirth.Name = "Rebirths"
Rebirth.Parent = leaderstats
end)

Bottom is leaderstats and top is the saving script

2 Likes

Instead of using the game.Players.PlayerRemoving for saving function, why not use game:BindToClose(function() function instead?

Here is your script, but edited with the game:BindToClose(function() function.

local ds = game:GetService("DataStoreService"):GetDataStore("SaveName")
local savevalue1
local lstats
local key

game.Players.PlayerAdded:Connect(function(plr)
	wait()

	key = "user_"..plr.UserId
	lstats = plr.leaderstats
	savevalue1 = plr.leaderstats.Rebirths

	local GetSaved

	local success, response = pcall(function()
		GetSaved = ds:GetAsync(key)
	end)

	if success then
		savevalue1.Value = GetSaved
	else
		local valuesForSaving = {savevalue1.value}
		ds:GetAsync(key, valuesForSaving)
	end
end)

game:BindToClose(function()
	local savevalue1 = lstats.Rebirths
	ds:SetAsync(key, {savevalue1.Value})
end)

This is my idea of it, you might have a similar idea to it.
Let me know if it works or not.

Ok, i’ll let you know if this fixes it!

Does not seem to work, I still get no errors.

Yup, I get this problem aswell. No errors, yet the leaderstats do not save.

Pretty annoying if I’m honest.

Why are you using a table to save your values?

If you’re doing that to save every stat, you can do so using different keys.

Try getting rid of the table and save the numerical value only. If it still doesn’t work, try printing the data you save and the one you get when calling GetAsync(), maybe you can see what’s wrong

Still nothing I have no idea why this script is failing.

And it seems a lot of people are having the same issue, did roblox change something or what?

Have you tried to run /console, go to Server side and type game.Players.(your name).leaderstats.Size.Value = 50. If I’m not wrong when you leave and rejoin it should do the job. If not then your code is completely wrong

Edit: in game from Roblox Site or App
Edit2: Mine did not want to save too, did this cmd from AlvinBlox and it did. DataStores are a pain

I am saving “rebirths” not size, not like it matters though, how is this supposed to help?

It should save, apply anything you want. If it doesn’t then your code is wrong. I don’t have my laptop to test your code + the command. Give it a try it’s just a tip it worked for me to see if it actually saves, replace size with rebirths then

So true, so true. I still don’t understand them fully and I’ve been scripting for 2 years

1 Like

best would be to have both in one script in ServerScriptService. Here is a little example you can use. to add more values just copy paste anything that has “Value” as the name.

	local	DatastoreService = game:GetService("DataStoreService")
store = DatastoreService:GetDataStore("Test")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	Value = Instance.new("NumberValue")     --for more values copy this block
	Value.Name = "Value"
	Value.Parent = leaderstats
	

end)

game.Players.PlayerAdded:Connect(function(player)
	
--for more values copy this from here
	local success, Cvalue = pcall(function() 
		return store:GetAsync(player.UserId.."-Value")
	end)

	if success then
		Value.Value = Cvalue or 0
	else
		warn("Error loading Value data for "..player.Name.." from datastore.")
	end
	-- to here
	

end)



	game.Players.PlayerRemoving:Connect(function(player)
		
			
		--for more values copy this from here
	store:SetAsync(player.UserId.."-Value", Value.Value) 

			print("Saved "..Value.Value.." Value to the datastore for "..player.Name..".")
 --to here
		end)

Why not use 1 script to load/save your data aswell as create the leaderstats?

This script is untested but I believe it should work. Let me know if there is any errors:

local DataStoreService = game:GetService("DataStoreService");
local Players = game:GetService("Players");

local DataStore = DataStoreService:GetDataStore("SaveName");

function GetData(Player)
	local Success,Result = pcall(function()
		return DataStore:GetAsync("user_"..Player.UserId);
	end)
	if not Success then -- Retry if request not successful
		wait(2); -- Delay between retries
		return GetData(Player)
	end
	return Result
end

function SaveData(UserId,SaveTable)
	local Success,Result = pcall(function() 
		return DataStore:SetAsync("user_"..UserId, SaveTable)
	end)
	if not Success then -- Retry if request not successful
		wait(2); -- Delay between retries
		return SaveData(UserId,SaveTable)
	end
	return print("Stats saved successfully")
end

function PlayerAdded(Player)
	local leaderstats = Instance.new("Folder");
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player

	local Size = Instance.new("IntValue",leaderstats);
	Size.Name = "Size"

	local Rebirth = Instance.new("IntValue",leaderstats);
	Rebirth.Name = "Rebirths"
	
	local Data = GetData(Player);
	if not Data then
		return
	end
	
	for _,v in pairs(leaderstats:GetChildren()) do
		local SavedStat = Data[v.Name];
		if not SavedStat then
			continue
		end
		v.Value = SavedStat;
	end
end

function PlayerRemoving(Player)
	local leaderstats = Player:FindFirstChild("leaderstats");
	if not leaderstats then
		return warn("No stats to save");
	end
	local SaveTable = {}
	for _,v in pairs(leaderstats:GetChildren()) do
		SaveTable[v.Name] = v.Value
	end
	SaveData(Player.UserId,SaveTable)
end

Players.PlayerAdded:Connect(PlayerAdded);
Players.PlayerRemoving:Connect(PlayerRemoving);

Keep in mind, this script saves all your stats in leaderstats. You can easily just add the ones you want to save to the SaveTable rather than looping through and adding all of them

1 Like