Problem with saving leaderstats

For some reason my leaderstats dont save whenever I leave the game, doesn anybody know why? I’m not sure why the code doesn’t work anymore, because it used to.

local DataStore = game:GetService("DataStoreService")
local powerds = DataStore:GetDataStore("PowerData")
local cashds = DataStore:GetDataStore("CashData")
local zoneds = DataStore:GetDataStore("ZoneData")


game.Players.PlayerAdded:Connect(function(player)

	local Cashid = player.UserId.."_Cash"
	local Powerid = player.UserId.."_Power"
	local Zoneid = player.UserId.."_Zone"

	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	
	local Power = Instance.new("IntValue",leader)
	Power.Name = "Power 💪"
	
	local PowerData
	local success, errormessage = pcall(function()
		PowerData = powerds:GetAsync(Powerid) 
	end)
	
	Power.Value = PowerData or 0

	local Cash = Instance.new("IntValue",leader)
	Cash.Name = "Cash 💰"
	
	local CashData
	local success, errormessage = pcall(function()
		CashData = cashds:GetAsync(Cashid) 
	end)
	
	Cash.Value = CashData or 0
	
	local Zone = Instance.new("IntValue",leader)
	Zone.Name = "Zone 🗺️"
	
	local ZoneData
	local success, errormessage = pcall(function()
		ZoneData = zoneds:GetAsync(Zoneid) 
	end)
	
	Zone.Value = ZoneData or 1

	if PowerData then
		Power.Value = PowerData
	else
		print("Error while getting your data")
		warn(errormessage)
	end
	
	if CashData then
		Cash.Value = CashData
	else
		print("Error while getting your data")
		warn(errormessage)
	end
	
	if ZoneData then
		Zone.Value = ZoneData
	else
		print("Error while getting your data")
		warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Cashid = player.UserId.."_Cash"
	local Powerid = player.UserId.."_Power"
	local Zoneid = player.UserId.."_Zone"

	local success, errormessage = pcall(function()
		powerds:SetAsync(Powerid, player.leaderstats["Power 💪"].Value)
	end)
	
	local success, errormessage = pcall(function()
		cashds:SetAsync(Cashid, player.leaderstats["Cash 💰"].Value)
	end)
	
	local success, errormessage = pcall(function()
		zoneds:SetAsync(Zoneid, player.leaderstats["Zone 🗺️"].Value)
	end)
end)
1 Like

You could be getting an error in your pcalls.

Currently if your pcalls error and it doesn’t save it doesn’t do anything or even let you know there was an error. Try adding at the bottom of your save code something like error(errormessage).

Pcalls should only be used to handle errors, if you don’t do anything about the errors you just cover them up and it would be better to just make a new thread most of the time.

-- Making a new thread, will stop the error from breaking the code but won't cover it up
task.spawn(function()
   -- code
end)

Though that’s hacky, it’s better to just use warnings.

You also should have some code that saves all the player’s data on game.BindToClose.

Programming datastore code correctly is hard, I’d recommend this resource:

It uses DataStore2 (which I would also recommend instead of programming the datastore code from scratch) and automatically saves the leaderstats. The only difference you need to make to your code is to make sure leaderstats has been loaded and let the resource create it for you. There is an example game showing how to use it I think.

If you are saving your data the traditional way and not using a module like profile service or datastore2 this script should work for you.

And also I question to why you use multiple data stores?

-- # Services
local Players = game:GetService('Players');
local DataStoreService = game:GetService('DataStoreService');

-- # Data Store
local DataStore = DataStoreService:GetDataStore('UserData', '1');

-- # Functions
local GetSavedData = function(player: Player): {}?
	local succ, Data = pcall(function()
		return DataStore:GetAsync(player.UserId);
	end);
	
	if succ and typeof(Data) == 'table' then
		return Data;
	end;
	
	return nil;
end;

local SaveData = function(player: Player, DataTable: {})
	local succ, msg = pcall(function()
		DataStore:SetAsync(player.UserId, DataTable);
	end);
end;

local PlayerAdded = function(player: Player)
	-- # Create Leaderstats
	local leaderstats = Instance.new('Folder');
	local Power = Instance.new('NumberValue');
	local Cash = Instance.new('NumberValue');
	local Zone = Instance.new('NumberValue');
	
	leaderstats.Name = 'leaderstats'
	Power.Name = 'Power 💪';
	Cash.Name = 'Cash 💰';
	Zone.Name = 'Zone 🗺️';
	
	-- # Set Default Values (If They Are Not 0)
	Zone.Value = 1;

    -- # Parent Everything
    Power.Parent = leaderstats;
    Cash.Parent = leaderstats;
    Zone.Parent = leaderstats;
    leaderstats.Parent = player;
	
	-- # Load Saved Data
	local Data = GetSavedData(player);
	
	if Data ~= nil then
		Power.Value = Data['Power'];
		Cash.Value = Data['Cash'];
		Zone.Value = Data['Zone'];
	end;
end;

local PlayerRemoving = function(player: Player)
	local leaderstats = player:FindFirstChild('leaderstats');
	local Power = leaderstats:FindFirstChild('Power 💪');
	local Cash = leaderstats:FindFirstChild('Cash 💰');
	local Zone = leaderstats:FindFirstChild('Zone 🗺️');
	
	local DataTable = {
		['Power'] = Power.Value;
		['Cash'] = Cash.Value;
		['Zone'] = Zone.Value;
	};
	
	SaveData(player, DataTable);
end;

-- # Connections
for _, v in ipairs(Players:GetPlayers()) do
	PlayerAdded(v);
end;

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

game:BindToClose(function()
	for _, v in ipairs(Players:GetPlayers()) do
		PlayerRemoving(v);
	end;
end);

thank you bro I love you, I swear I literally don’t understand anything about ur code it’s so complex, but it works so thanks so much dude!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.