Add another data save to a currency

How would I go about to add another data save to my currency "Cash"

I tried many ways but I got confused.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

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

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

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

		local data
		local success, errormessage = pcall(function()
				data = myDataStore:GetAsync(player.UserId.."-MissionsCompleted")
		end)

		if success then
			 	MissionsCompleted.Value = data
		else
				print("ErrorFetchingData")
				warn(errormessage)
		end
end)

game.Players.PlayerRemoving:Connect(function(player)
		local success, errormessage = pcall(function()
			myDataStore:SetAsync(player.UserId.."-MissionsCompleted", player.leaderstats.MissionsCompleted.Value)
		end)

		if success then
			print("DataStoreSuccess")
		else
			print("DataStoreError")	
			warn(errormessage)
		end
end)

the text above is for saving the “missionscompleted” data

1 Like

I suggest you read the documentation first

https://developer.roblox.com/articles/Data-store

If you want to add another datastore then just create another Datastore and use GetAsync, check if it exists for the player, if not then SetAsync to new player value, then on player leave, SetAsync to both datastores :slight_smile:

1 Like
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local myDataStore2 = DataStoreService:GetDataStore("myDataStore2")

im pretty sure this will work to create a new datastore: just name another datastore in the getdatastore function