local PlayerLevel = game:GetService("DataStoreService"):GetDataStore("PlayerLevel")

local levelManagement = Instance.new("Smoke", game.ServerStorage)
levelManagement.Name = "LevelStorage"

game.Players.PlayerAdded:connect(function(player)
	local playerKey = "player_" .. player.userId

	local levelStorage = Instance.new("NumberValue", game.ServerStorage.LevelStorage)
	levelStorage.Name = player.Name
	levelStorage.Value = PlayerLevel:GetAsync(playerKey) or 1

	local leaderboard = Instance.new("IntValue")
	leaderboard.Name = "leaderstats"
	leaderboard.Parent = player

	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderboard
	level.Value = levelStorage.Value
	
	if (levelStorage) then
		levelStorage.Changed:connect(function()
			level.Value = levelStorage.Value
		end)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local levelStorage = game.ServerStorage.LevelStorage:FindFirstChild(player.Name)
	local playerKey = "player_" .. player.userId

	if (levelStorage) then
		PlayerLevel:UpdateAsync(playerKey, function(old)
			local new = old or 1

			new = new + (levelStorage.Value - old)

			return new
		end)
	end
end)

Hey, so the above is my code. Now it works fine, however, when I leave the game and join back it doesn't show my correct level. It just shows that my level is 1 on the scoreboard, and yes I made sure it was different before I left.

2 comments

0

Are you trying to make a datastore saving leaderboard for saving leaderstats such as in game currency, or in this case, levels?

0

I'm trying to save levelStorage.Value in the "PlayerLevel" data store.