Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to add a second leaderstat??

Hi, I am having issues with my leaderstats. I have a datastore for money. It all works perfectly. So, I decided to duplicate the script and change money to gems and renamed the datastore to GEMSDataStore. Issue is the leaderstats only display one of the currency and I want it to display both.

Code:

local DSS = game:GetService("DataStoreService")

local DataStore = DSS:GetDataStore("GEMSDataStore")





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

	local leaderstats = Instance.new("yum")

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player

	

	local Gem = Instance.new("IntValue")

	Gem.Name = "Gem"

	Gem.Parent = leaderstats

	

	local data

	local success, errorMessage = pcall(function()

		data = DataStore:GetAsync(player.UserId)

	end)

	if success and data ~= nil then

		print("Data successfully loaded!")

		Gem.Value = data.Gem

	else

		warn(errorMessage)

	end

end)



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

	local leaderstats = player.leaderstats

	local data = {

		Gem = leaderstats.Gem.Value; }

	local success, errorMessage = pcall(function()

		DataStore:SetAsync(player.UserId,data)

	end)

	if success then

		print("Data successfully saved!")

	else

		warn(errorMessage)

	end

end)

1 comment

0

In the instance when I was trying to fix this I put yum in the brackets to see if that will work that's why its there on line 11

1 answer

✓ Accepted answer

I tested and it worked, put a Normal Script in the ServerScriptService

local DSS = game:GetService("DataStoreService")
local CashData = DSS:GetDataStore("CashData")
local GemsData = DSS:GetDataStore("GemsData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Cash"
	
	local gems = Instance.new("IntValue", leaderstats)
	gems.Name = "Gems"
	
	local playerId = "Player_"..player.UserId
	
	local cashdata
	local gemsdata
	local success, errormessage = pcall(function()
		cashdata = CashData:GetAsync(playerId)
		gemsdata = GemsData:GetAsync(playerId)
	end)
	
	if success then
		cash.Value = cashdata
		gems.Value = gemsdata
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerId = "Player_"..player.UserId
	
	local cashdata = player.leaderstats.Cash.Value
	local gemsdata = player.leaderstats.Gems.Value
	
	local success, errormessage = pcall(function()
		CashData:SetAsync(playerId, cashdata)
		GemsData:SetAsync(playerId, gemsdata)
	end)
	
	if success then
		print("Saved")
	else
		print("There was an error")
		warn(errormessage)
	end
end)

2 comments

0

Thank u This helps me a lot and I also see now how to add 2 datastores in leaderstats using 1 script.

0

You're welcome

This discussion is preserved as it was. New answers, comments, and votes are closed.