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?
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....
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.
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.
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.