How do you use datastore 2 with a lot of values?

I only have around 3 months of scripting experience, so this is quite confusing for me.

I watched some tutorials on datastore 2 (AlvinBlox’s and some other one), I got the hang of it, but these tutorials showed it with only one value, like points.

My game has a lot of values, like money, gears stored, maximum gear capacity, exp, needed exp, level, health, max health, and many bool values like ownsGear1 all the way up to ownsGear17 (many more to come in the future).

Now I’m confused, do I make separate keys for all the values, and combine them into one master key, or do I make separate keys for tables that contain all the stat values (money, gears, health etc.) and tables that contain all the owned gears (ownsGear1-17+) and so on.

I’m not sure, but I have a gut feeling that having one :OnUpdate() event that updates ALL of these values is not the right way to go around this.
Are there any good tutorials on this? I couldn’t find anything that could help me with this.

Personally I’ve never used DataStore2 so I don’t really know how its OnUpdate works, but I can answer this question:

The best way is to have a stats table for every player, it could look like this:

{
    Money = 100,
    Health = 200,
    Gears = {"Epic Sword", "I like trains"},
}

and save it under a single datastore key (usually preferred to be the UserId of the player with something appended before it, like "Player_" .. plr.UserId).

3 Likes

i watched this to get started setting up my DataStore2. This may help

2 Likes

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.

4 Likes

Thanks for your reply! I can’t get it to work, though. I think I’ve done everything like you, but when I fire the IncrementMoney function, this line in the module script errors:

local moneyDataStore = dataStore2("money", player)

This is the error message:

ServerScriptService.Stats.dataStore2:5: attempt to call a nil value

Make sure that you have used:

dataStore2.Combine("DATA", "Money")
In a ServerScript with the correct capitalization, else that value will not exist and return an error.

I had done that. Also, in the module script, in the same line, “dataStore2” is underlined orange, it says "Unknown global ‘dataStore2’ ". I tried pasting this into the module script but it didn’t work:
local dataStore2 = require(game:GetService("ServerScriptService").DataStore2)

Make sure that DataStore2 is the source code for it following these instructions.

It’s always been there. I’ll try redoing it.

Are you sure you did Datastore2.Combine with a “.” and not a “:”?

Yup, it’s a “.”, and not a “:”

Mind if I ask why you used self here instead of player? (I’m relatively new to this type of stuff)