How to use DataStore2.Combine?

After re-reading the DataStore 2 API, I realized that I’ve been using DataStore2 wrong this entire time. I’ve been saving each stat individually instead of saving them all as one table. My last project faced throttling as a result.

You should never use data stores without combining them or at the very least, replicating the behavior by creating one large dictionary yourself! Combined data stores will soon be the default way to use DataStore2.

With my new project I’m trying to optimize the code as much as possible but I simply don’t understand how to. (I’m notoriously bad at scripting datastores, which is why I gravitated to DataStore2 in the first place.

I’ve read these entire threads and I still don’t get it:

Links

Saving tables in datastore2?
How to datastore2 more than 1 variable?
Datastore 2 tables!

All help is greatly appreciated!

My Server Script:

local DataStore2 = require(script.Parent.DataStore2) -- Does it matter where I store the module?
local savedPlayerValues = require(game:GetService("ReplicatedStorage").ModuleScript) 

game.Players.PlayerAdded:Connect(function(player)
	
	for DataStoreName,v in pairs(savedPlayerValues) do
		local datastore = DataStore2(DataStoreName, player)
		local Location = v.Location
		if v.Location ~= "Player" then
			if player:findFirstChild(v.Location) then
				Location = player[v.Location]
			else
				local folder = Instance.new("Folder", player)
				folder.Name = v.Location
				Location = folder 
			end
		end
		
		if v.Location == "Player" then
			Location= player
		end
		
		local valueInstance = Instance.new(v.ValueType, Location)
		valueInstance.Name = DataStoreName 
		valueInstance.Value = v.DefaultValue
		
		if datastore:Get() ~= nil then 
			valueInstance.Value = datastore:Get() 
		end
		
		valueInstance.Changed:connect(function() 
			datastore:Set(valueInstance.Value)
		end)
	end
end)

-- Lucid_Reese ;p

My Module Script:

local savedPlayerValues = {
	["ExampleStatOne"] = {
		["Location"] = "savedValues", 
		["ValueType"] = "IntValue",
		["DefaultValue"] = 1100,
	},
	["ExampleStatTwo"] = {
		["Location"] = "savedValues",
		["ValueType"] = "BoolValue",
		["DefaultValue"]= false,
	}, 
	["ExampleStatThree"] = {
		["Location"] = "savedValues",
		["ValueType"] = "StringValue",
		["DefaultValue"]= "Equipped",
	},
	["ExampleStatFour"] = {
		["Location"] = "savedValues",
		["ValueType"] = "IntValue",
		["DefaultValue"]= 1337,
	},
	["ExampleStatFive"] = {
		["Location"] = "savedValues",
		["ValueType"] = "IntValue",
		["DefaultValue"]= 50,
	},
}
return savedPlayerValues
3 Likes
local DataStore2 = require(script.Parent.DataStore2)
local savedPlayerValues = require(game:GetService("ReplicatedStorage").ModuleScript) 

-- put outside the Players.PlayerAdded event
for datastoreName in pairs(savedPlayerValues) do
	Datastore2.Combine("PlayerData", datastoreName)
end

That’s all you need to do!

Datastore2 will automatically combine every key (ExampleStatOne, ExampleStatTwo, etc) into one large dictionary with the masterKey “PlayerData”. You can continue getting and setting each stat individually like you are now, and Datastore2 will do the rest internally.

3 Likes