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

How do I add another IntValue to the leaderboard?

I watched a video on how to add currency, and now i want to add 2 currencies. I don't exactly know how.. I am very new to this if i could get some help that would be great

local currencyName = "Zimo"
local DataStore = game:GetService("DataStoreService"):GetDataStore("Currency")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder

	local ID = currencyName.."-"..player.UserId
	local savedData = nil
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)	
	
	if savedData ~= nil then
		currency.Value = savedData
		print("Data Loaded")
	else
		--New player--
		currency.Value = 750
		print("New player to game")
	end	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	-- When game is ready to shutdown
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("Server shuting down")
		end
	end
	
	wait(5)
end)

Also if there is a better way to do this im open for it.

1 answer

Just use the same code you used before

local currency = Instance.new("IntValue") --- change currency to the other currency
    currency.Name = currencyName -- Change The Name
    currency.Parent = folder

If You Wanted Cash You'd Do

local Cash = Instance.new("IntValue")
    Cash.Name = Cash
    Cash.Parent = folder

The Datastore I use is

game.Players.PlayerRemoving:Connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."leaderstats")
	local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #statstorage do
		datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
		print("Saved Data Number ("..i..")")
	end
	print("Stats Successfully Saved!")
end)

game.Players.PlayerAdded:Connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."leaderstats")
	player:WaitForChild("leaderstats")
	wait(1)
	local stats = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #stats do
		stats[i].Value = datastore:GetAsync(stats[i].Name)
		print("stats number ("..i..") has been found")
	end
end)

Works Fairly Well

5 comments

0

Would I make a new script? cause if i put another IntValue it breaks the data save for the other currency

0

Used have to make another Datastore because the current Datastore you're using only saves one currency

0

Does it work for you? If so can you accept the answer?

0

Sadly no I tried adding another script but it doesnt load half of the time idk why I could be doing something wrong..

0

Did you put the Datastore in a serverscript?

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