How to make this leaderboard have 'stages' in it?

Hey there! I have two currencies running alongside each other, and I’m currently making an obstacle course that involves grappling hooks. However, I want to make it to where the player’s data can be saved. Is there anyway to implement this into my game with the current script I have now?

Here it is, if anyone can help me. Thank you.

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character

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

	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Gold"

	local void = Instance.new("IntValue")
	void.Parent = stats
	void.Name = "Voids"

	if CashStore:GetAsync("GOLD"..player.UserId) ~= nil then
		cash.Value = CashStore:GetAsync("GOLD"..player.UserId)
	else
		cash.Value = 0
	end

	if CashStore:GetAsync("VOID"..player.UserId) ~= nil then
		void.Value = CashStore:GetAsync("VOID"..player.UserId)
	else
		void.Value = 0
	end

	cash.Changed:connect(function(Val)
		CashStore:SetAsync("GOLD"..player.UserId, Val)
	end)

	void.Changed:connect(function(Val)
		CashStore:SetAsync("VOID"..player.UserId, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)

EDIT: Should clarify that I want their CHECKPOINTS to be saved. Is there a way to add in ‘stages’ in this script?

You could easily save the stages they have cleared (or their current stage,) in a datastore, as you have already done for the two currencies. I’m unsure what your question is. Are you asking how to add it to the leaderboard so every player could see it?

Yes! Exactly that! I’m only wondering on how to do that, as that datastore often overrides the current one I already have. Then it makes the game unplayable.

The datastore shouldn’t overwrite the leaderboard unless you set the leaderstats to equal the datastore value when they update. I would also recommend only updating the datastore with the stages when the player disconnects, that way it doesn’t get stuck and only load that datastore value when the player connects.

1 Like

You need to handle the leaderstats creation and DataStore saving/loading from within a single server script.

1 Like

Okay! For those that are interested in my progress- I was able to implement the stages system into the game! Here is an open source if anyone wants it. Thank you to those that replied. :)!

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character

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

	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Gold"

	local void = Instance.new("IntValue")
	void.Parent = stats
	void.Name = "Voids"
	
	local stage = Instance.new("IntValue")
	stage.Parent = stats
	stage.Name = "Stage"
	
-- Adding in the leaderstats for both two currencies and stages.

	if CashStore:GetAsync("GOLD"..player.UserId) ~= nil then
		cash.Value = CashStore:GetAsync("GOLD"..player.UserId)
	else
		cash.Value = 0
	end

	if CashStore:GetAsync("VOID"..player.UserId) ~= nil then
		void.Value = CashStore:GetAsync("VOID"..player.UserId)
	else
		void.Value = 0
	end
	
	if CashStore:GetAsync("STAGE"..player.UserId) ~= nil then
		stage.Value = CashStore:GetAsync("STAGE"..player.UserId)
	else
		stage.Value = 0
	end

-- The leaderboards finally showing the stages and two currencies.

	cash.Changed:connect(function(Val)
		CashStore:SetAsync("GOLD"..player.UserId, Val)
	end)

	void.Changed:connect(function(Val)
		CashStore:SetAsync("VOID"..player.UserId, Val)
	end)
	
	stage.Changed:connect(function(Val)
		CashStore:SetAsync("STAGE"..player.UserId, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)

-- Saving the leaderboard, including the checkpoints when the player leaves.