Change Leaderstats to 2 instead of 1?

Hi everyone I’M currently working on a Leaderstats System and rn I got the issue that I can only show 2 Leaderstats with a codeline but it doesn’t save or only 1 with the codeline I’ma show now and it does save. What I need right now is a way I can let both show with saving(this is the line:function setupLeaderstats(plr:Player, savedCash:number))

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("DATA")

local mps = game:GetService("MarketplaceService")

local devProducts = require(game:GetService("ReplicatedStorage"):WaitForChild("DeveloperProducts"))


function saveData(plr:Player)
	
	if not plr:FindFirstChild("DATA FAILED TO LOAD") then
		
		local plrCash = plr.leaderstats.Cash.Value
		
		local success, err = nil, nil
		
		while true do
			
			success, err = pcall(function()
				ds:SetAsync(plr.UserId .. " - Cash", plrCash)
			end)
			
			if not success then
				warn(err)
				task.wait(0.01)
			else
				break
			end
		end
	end
end

function loadData(plr:Player)
	
	local dataFailedWarning = Instance.new("StringValue")
	dataFailedWarning.Name = "DATA FAILED TO LOAD"

	local success, plrData = nil, nil
	
	while true do
		success, plrData = pcall(function()
			return ds:GetAsync(plr.UserId .. " - Cash")
		end)
		task.wait(0.02)
		if not success then
			dataFailedWarning.Parent = plr
			warn(plrData)
		else
			break
		end
	end
	dataFailedWarning:Destroy()
	
	return plrData
end



function setupLeaderstats(plr:Player, savedCash:number)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent =plr

	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0 
	cash.Parent = leaderstats
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0 
	wins.Parent = leaderstats
end

function handlePurchase(purchaseInfo)
	
	local plr = game.Players:GetPlayerByUserId(purchaseInfo.PlayerId)
	
	if not plr or plr:FindFirstChild("DATA FAILED TO LOAD") then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	local productID = purchaseInfo.ProductId
	
	if devProducts[productID] then
		plr.leaderstats.Cash.Value += devProducts[productID].Cash
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end


game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		saveData(plr)
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	
	local plrData = loadData(plr)
	setupLeaderstats(plr, plrData)
end)





mps.ProcessReceipt = handlePurchase
1 Like

I’d recommend looking up tutorials on YT or on DevForum to setup leaderstats and data saving. The simple way to expand from 1 saved value to 2, would be to use 2 seperate datastores for each value, especially if they are to be used for leaderboards.

1 Like

I did but it only shows how to code 2 or 1 with save but nothing really that helped me so I thought dev forum is better(I thought my issue is easier than I thought tbh)

Just use data store 2 module for ingame values.

How to use DataStore2 - Data Store caching and data loss prevention - Resources / Community Tutorials - Developer Forum | Roblox

1 Like

Use profile service or save the values in a table where the key is the name of the stat and the value the value of the stat then encode it to Json with the http service and save it to the data store. Then to load the data decode the data to a table using jsondecode with http service. Then for money you use data.Money etc…

1 Like

I swear I watched a vid from someone called (Credits:RealAlphaMango on Youtube and I somehow fixed this now idk who to give the solution mark cuz y’all tried to help

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