What I do with DS2 (and I am saving a lot of values) is to create a module script in ServerScriptService
with 3 functions per value - a set function, a get function and a increment function. This is a great base for quickly changing values from a server script. I also find it useful to set a remote function up with a ServerScript
for getting values form the client, e.g. Money.
Module Script
local dataHandler = {}
function dataHandler.IncremenGold(player, value)
local defaultValue = 500
local goldDataStore = dataStore2("Gold", player)
goldDataStore:Increment(value, defaultValue)
return goldDataStore:Get(defaultValue)
end
function dataHandler.GetGold(player, value)
local defaultValue = 500
local goldDataStore = dataStore2("Gold", player)
goldDataStore:Set(value)
return goldDataStore:Get(defaultValue)
end
function dataHandler.GetGold(player)
local defaultValue = 500
local goldDataStore = dataStore2("Gold", player)
return goldDataStore:Get(defaultValue)
end
-- Add all other values
return dataHandler
Then in a server script
local dataStore2 = require(script.Parent.DataStore2) -- DS2 source
local dataSaver = require(script.DataModule) -- Module script above
dataStore2.Combine("DATA", "Money", "Gold") -- Prevents throttling, add all values here, DATA is the 'key' for all of the data stored, if you change it all of the saved data will be wiped.
Then you can use either to easily get the data values from the client (e.g. updating UI etc.)
frontendRemotes.RequestMoney.OnServerInvoke = function(self) -- Get Money
return dataSaver.GetMoney(self)
end
or
frontendRemotes.RequestData.OnServerInvoke = function(self, dataType) -- Get Money
if dataType == "Money" then
return dataSaver.GetMoney(self)
elseif dataType == "Gold" then
return dataSaver.GetGold(self)
end
end
Now all you have to do to update any values is call the module script and fire the correct function from the server. e.g.
local dataSaver = require(script.DataModule)
dataSaver.GetIncrementGold(self, 250) -- Adds 250 gold
Note: Data Store 2 automatically caches and saves data so there is no need for anything along the lines of hiddenstats like you need for normal data stores. There is also no need for saving player data when they leave.
If you have any questions don’t hesitate to ask - I hope that this clears things up for you however I am not the best at explaining things so I won’t be offended if you don’t understand.