Cannot add 2 leaderstats that save using DataStoreService

I’m trying to add a way to exchange an amount of one leaderstat to a different amount of another leaderstat, issue is this script that saves the leaderstat using Data Stores makes it difficult to know how to add another one, adding it by doing

local stat = "OtherLeaderstatName"

does not work, even while specifying the existence of two leaderstats, it only uses one (most of the time the one I just added), making a copy of the script once again makes it only use one, this script seems too specific to find a post of it, if anyone knows how to add 2 leaderstats that save in the datastore or a way to add another leaderstat to this script I would gladly appreciate the help. Here’s the script:

local stat = "Coins"
local startamount = 100


local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue",leader)
Cash.Name = stat
Cash.Value = ds:GetAsync(player.UserId) or startamount 
ds:SetAsync(player.UserId, Cash.Value)
Cash.Changed:connect(function()
ds:SetAsync(player.UserId, Cash.Value)
end)
end)

game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)

You want to save the data of both leaderstats, right? It is not possible to save both since the script would be shared when calling both leaderstats because they have the same name

To save data, the folder does not necessarily have to be called “Leaderstats”

Sorry, the page got bugged… This is a leaderstats and the data is saved with tables, in this way you will only occupy a dataStore and you will not need another, you only need to change the name and reconfigure the rest local DataStore =

game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local ValueStats = {
		{type = "IntValue", name = "Coins"},
		{type = "IntValue", name = "Gems"},
	}
	for _, value in pairs(ValueStats) do
		local val = Instance.new(value.type)
		val.Name = value.name
		val.Parent = leader
	end
	local data = nil
	local success, err = pcall(function()
		data = ds:GetAsync(player.UserId)
	end)
	if success and data ~= nil then
		leader:WaitForChild("Coins").Value = data.Coins
		leader:WaitForChild("Gems").Value = data.Gems
	else
		leader:WaitForChild("Coins").Value = 100
		leader:WaitForChild("Gems").Value = 0
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local DataTable = {
		Coins = player.leaderstats.Coins.Value;
		Gems = player.leaderstats.Gems.Value;
	}

	local Success, err = pcall(function()
		ds:SetAsync(player.UserId, DataTable)
	end)

	if Success then
		print("Se ha guardado correctamente / Has been saved successfully")
	else
		print("hubo un error al guardar / there was an error saving")
		warn(err)
	end
end)

Thanks for the help! I’ve already found a solution to this but I’ll mark that as a solution if anyone stumbles upon this post looking for help for the same reason.

1 Like