3 stats leaderboard script (not saving)

Hi! So I’m making a lifting simulator game with the leaderboard stats (Strength, KillStreak, and Rank)

Whenever I change any of these stats, it doesn’t save when I rejoin the game. To lift (increases Strength) I use a local script as well as for rebirthing (changing rank) is in a local script too.

Here is the script I use to create the leaderstats, it used to save in a few of my other games, I don’t get why it wouldn’t be saving now.

local DataStoreService = game:GetService("DataStoreService")

local data = DataStoreService:GetDataStore("Strength_2")

local DatedPlayer = DataStoreService:GetDataStore("KillStreak_2")

local PlayerData = DataStoreService:GetDataStore("Rank_2")




local function onPlayerJoin(player)  

	local leaderstats = Instance.new("Folder") 

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local strength = Instance.new("NumberValue") 

	strength.Name = "Strength"

	strength.Parent = leaderstats
	
	
	local kills = Instance.new("IntValue") 

	kills.Name = "KillStreak"

	kills.Parent = leaderstats
	
	
	local rank = Instance.new("StringValue") 

	rank.Name = "Rank"

	rank.Parent = leaderstats
	


	local playerUserId = "Player_" .. player.UserId  

	local StrengthStat = data:GetAsync(playerUserId)  
	
	local KillStat = DatedPlayer:GetAsync(playerUserId)  
	
	local RankStat = PlayerData:GetAsync(playerUserId)  



	if StrengthStat then

		strength.Value = StrengthStat['Strength']


	else

		

		strength.Value = 0


	end
	
	if KillStat then


		kills.Value = KillStat['Killstreak']

	else

		


		kills.Value = 0

	end
	
	if RankStat then


		rank.Value = RankStat['Rank']

	else

		


		rank.Value = "Meteoroid"

	end

end



local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end



local function onPlayerExit(player)  



	local player_stats = create_table(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		data:SetAsync(playerUserId, player_stats) 
		
		DatedPlayer:SetAsync(playerUserId, player_stats) 
		
		PlayerData:SetAsync(playerUserId, player_stats) 


	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

Thank you!!

I don’t know why exactly you are using multiple data stores, use one data store and store all their data in a table.

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

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

-- # Functions
local function PlayerAdded(player: Player)
	local leaderstats = Instance.new('Folder');
	leaderstats.Name = 'leaderstats';
	
	local Streak = Instance.new('NumberValue');
	Streak.Name = 'KillStreak';
	
	local Strength = Instance.new('NumberValue');
	Strength.Name = 'Strength';
	
	local Rank = Instance.new('StringValue');
	Rank.Name = 'Rank';
	
	-- # Set Default Values
	Rank.Value = 'Meteoroid';
	
	-- # Get Saved Data (If any)
	local succ, PlayerData = pcall(function()
		return DataStore:GetAsync(player.UserId);
	end);
	
	if succ and typeof(PlayerData) == 'table' then
		Streak.Value = PlayerData['Streak'];
		Strength.Value = PlayerData['Strength'];
		Rank.Value = PlayerData['Rank'];
	end;
	
	Streak.Parent = leaderstats;
	Strength.Parent = leaderstats;
	Rank.Parent = leaderstats;
	leaderstats.Parent = player;
end

local function PlayerRemoving(player: Player)
	local leaderstats = player:FindFirstChild('leaderstats');
	local Streak = leaderstats:FindFirstChild('KillStreak');
	local Strength = leaderstats:FindFirstChild('Strength');
	local Rank = leaderstats:FindFirstChild('Rank');
	
	local DataTable = {
		['Streak'] = Streak.Value;
		['Strength'] = Strength.Value;
		['Rank'] = Rank.Value;
	};
	
	local succ, err = pcall(function()
		DataStore:SetAsync(player.UserId, DataTable);
	end);
	
	if not succ then
		warn(err);
	end;
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);
1 Like

Thanks so much, I forgot that the script only works if the game is public, same for your script. Thanks again!

1 Like

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