Data Storage Gold Question

Hi I have a data storage script that only keeps track of gold collected. I want to add ruby’s and gems to the tracked items. Can I add it to the current script or can I do a separate script?

local dss = game:GetService("DataStoreService")
local mydatastore = dss:GetDataStore("mydatastore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	local gold = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	gold.Name = "Gold"
	gold.Parent =leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = mydatastore:GetAsync(player.UserId.."-Gold")
	end)
	if success then
		gold.Value = data
	else
		print ("Error Fetching Data")
		warn (errormessage)		
	end
	game.Players.PlayerRemoving:Connect(function(player)
		local success, errormessage = pcall (function()
			mydatastore:SetAsync(player.UserId.."-Gold",player.leaderstats.Gold.Value)
		end)
		if success then
			print ("Stats Saved")
		else
			print ("Saving Failed")
		end
end)
end)

I was thinking about just adding a second scrip and modifying it like this.

local dss = game:GetService("DataStoreService")
local mydatastore = dss:GetDataStore("mydatastore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = game.Players.leaderstats
	local gem = Instance.new("IntValue")  --- line changed
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	gem.Name = "Gems"                       --- line changed
	gem.Parent =leaderstats                --- line changed
	
	local data
	local success, errormessage = pcall(function()
		data = mydatastore:GetAsync(player.UserId.."-Gem")  --- line changed
	end)
	if success then
		gem.Value = data                  --- line changed
	else
		print ("Error Fetching Data")
		warn (errormessage)		
	end
	game.Players.PlayerRemoving:Connect(function(player)
		local success, errormessage = pcall (function()
			mydatastore:SetAsync(player.UserId.."-Gem",player.leaderstats.Gems.Value)    --- line changed
		end)
		if success then
			print ("Stats Saved")
		else
			print ("Saving Failed")
		end
end)
end)

Would adding this script accomplish the same thing? Or is it better to add to the first script?

Thanks

1 Like

Why does this script not add a new IntValue to leaderstats called gems?

local dss = game:GetService("DataStoreService")
local mydatastore = dss:GetDataStore("mydatastore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = game.Players.leaderstats
	local gem = Instance.new("IntValue")  --- line changed
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	gem.Name = "Gems"                       --- line changed
	gem.Parent =leaderstats                --- line changed
	
	local data
	local success, errormessage = pcall(function()
		data = mydatastore:GetAsync(player.UserId.."-Gem")  --- line changed
	end)
	if success then
		gem.Value = data                  --- line changed
	else
		print ("Error Fetching Data")
		warn (errormessage)		
	end
	game.Players.PlayerRemoving:Connect(function(player)
		local success, errormessage = pcall (function()
			mydatastore:SetAsync(player.UserId.."-Gem",player.leaderstats.Gems.Value)    --- line changed
		end)
		if success then
			print ("Stats Saved")
		else
			print ("Saving Failed")
		end
end)
end)

You could have a separate script and that would work but is discouraged because too many event listeners (Players.PlayerAdded) can add some lag. You can keep doing the thing your doing in your script but just do each step x3 for each object.

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

--gold
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Parent =leaderstats

--ruby
	local ruby= Instance.new("IntValue")
	ruby.Name = "ruby"
	ruby.Parent =leaderstats
	
	local GoldData
    local RubyData
	local success, errormessage = pcall(function()
		GoldData = mydatastore:GetAsync(player.UserId.."-Gold")
		RubyData = mydatastore:GetAsync(player.UserId.."-Ruby")
	end)
	
etc....

I will try this thank you. Do you think I should use something other than players.PlayerAdded?

Player data should always be saved as a dictionary under a single key. Never use multiple DataStores unless you absolutely need to. You should be aware of the limitations of DataStores.

There are a couple of resources out there which can help you do this (e.g. ProfileService), but if you do not want to use them then I do encourage doing a bit of research into the idea of dictionaries so you can store player data that way instead.

No idea what this means because it’s not true. The problematic part here is the way DataStores are being used, not the amount of connections being made. You are perfectly capable of connecting to a signal multiple times and having multiple handlers acting on it for different systems.

2 Likes

How does this look.

local dss = game:GetService("DataStoreService")
local mydatastore = dss:GetDataStore("mydatastore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	local gold = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	gold.Name = "Gold"
	gold.Parent =leaderstats
	
	local gems = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	gems.Name = "Gems"
	gems.Parent =leaderstats
	
	local GoldData
	local GemsData
	local success, errormessage = pcall(function()
		GoldData = mydatastore:GetAsync(player.UserId.."-Gold")
		GemsData = mydatastore:GetAsync(player.UserId.."-Gems")
	end)
	if success then
		gold.Value = GoldData
		gems.Value = GemsData
	else
		print ("Error Fetching Data")
		warn (errormessage)		
	end
	game.Players.PlayerRemoving:Connect(function(player)
		local success, errormessage = pcall (function()
			mydatastore:SetAsync(player.UserId.."-Gold",player.leaderstats.Gold.Value)
			mydatastore:SetAsync(player.UserId.."-Gems",player.leaderstats.Gold.Value)
		end)
		if success then
			print ("Stats Saved")
		else
			print ("Saving Failed")
		end
end)
end)

Yes that should work, what colbert said is also important, It isnt mandatory to use dictionarys and save under one key, but it is highly recommended to do so. If you choose to use the multiple Datastores, you may come across issues as each server can only access the DataStore so many times in a certain time span.

If you want to optimise as your DataStore to as good as it could be, You can use community made modules such as ProfileSerivce or DataStore2.

I must be missing something because this script just copies the number of gold and puts that many gems in the intvalue.

Its not using two different values.

Of course this would error because there is notthibf inside GemsData(This May happen if you only add it recently). You have to do a conditional check to check if has a data or not, if not, then use like a starting gem amount, otherwise use whatever that’s in the data.

Another problem is the way you are saving your data.

See the difference? When you save your Gems, you used the Gold.Value.