DataStore2 Help

So recently I have been messing around with the DataStore2 module. I am trying to understand it, but to me it is confusing. So could someone help me out on it? All I want is to make a simple leaderstats script with multiple values.

I don’t want to have to go through all of my scripts that add/subtract leaderstats and change them to Increment() too.

Current Script

local DataStore2 = require(game.ServerScriptService.DataStore2)

local defaultValue = 1
local defaultValue2 = 0

DataStore2.Combine("money", "claps", "level", "plus", "keylvl")

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	
	local Claps = Instance.new("IntValue",leaderstats)
	Claps.Name = "Claps"
	
	local totalClaps = Instance.new("IntValue",plr)
	totalClaps.Name = "totalClaps"
	
	local Level = Instance.new("IntValue",leaderstats)
	Level.Name = "Level"

	local Plus = Instance.new("NumberValue",plr)
	Plus.Name = "Plus"
	
	local Money = Instance.new("IntValue",leaderstats)
	Money.Name = "Money"

	local KeyLevel = Instance.new("IntValue",plr)
	KeyLevel.Name = "KeyLevel"
	
	local function moneyUpdate(updatedValue)
		moneyStore = DataStore2("money", plr)
		Money.Value = moneyStore:Get(updatedValue)
	end
	
	moneyUpdate(defaultValue2)
	
	moneyStore:OnUpdate(moneyUpdate)	
end)


If you want to just make one for me so that I can read it go here - DataStore2 Scripter

I would look at this tutorial by @Alvin_Blox

I watched it, but it doesn’t make much sense to me on how you can save it without the increment stuff.

It all saves automatically. That’s the nice feature with DataStore2.

So I don’t have to change anything in my code that adds/subtracts leaderstats?

I’m confused as to what you need…?

Well I don’t know why my code isn’t working in the first place. And secondly, do I have to change my previous scripts that would increase a player’s leaderstats to a system with Increment()

Yes, taht’s how you would add or subtract. for example: datastore:Increment(value)

Someone told me I didn’t need to change any of my previous scripts that would do something like plr.leaderstasts.Points.Value = plr.leaderstasts.Points.Value + 5 (i am switching from DataStore to DataStore2)

You would need to because Datastore2 has different functions than Datastore

Is there a way you can save whenever the player leaves? Or make a save system that loops?

It’s completely automatic. Once the player leaves, you can look in the output and see that whatever datastore saved.

So is my script correct? Is there any errors?

That’s for you to find out. Are there any errors?

I meant as in “Do you see any noticeable mistakes.” I will check again but I don’t think there are any errors.

What happens is that nothing changes on the leaderstats when I click. So is my problem here that I am not using Increment() ?

Nothing changes. The script will detect that nothing has happened and not change any values

1 Like

Ok so just to check, am I doing this the right way?

Clap Script

local DataStore2 = require(game.ServerScriptService.DataStore2)

game:GetService("ReplicatedStorage").GiveCoins.OnServerEvent:Connect(function(player)
	local newClapValue = player.Plus.Value * 10 + player.leaderstats.Claps.Value
	local newtClapValue = player.Plus.Value * 10 + player.totalClaps.Value
	print(player.Plus.Value)
	
	local clapStore = DataStore2("claps", player)
	local TclapStore = DataStore2("tclaps", player)
	
	clapStore:Increment(newClapValue, 0)
end)

DataStore2 Script

local DataStore2 = require(game.ServerScriptService.DataStore2)

local defaultValue = 1
local defaultValue2 = 0

DataStore2.Combine("money", "claps", "level", "plus", "keylvl", "tclaps")

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	
	local Claps = Instance.new("IntValue",leaderstats)
	Claps.Name = "Claps"
	
	local totalClaps = Instance.new("IntValue",plr)
	totalClaps.Name = "totalClaps"
	
	local Level = Instance.new("IntValue",leaderstats)
	Level.Name = "Level"

	local Plus = Instance.new("NumberValue",plr)
	Plus.Name = "Plus"
	
	local Money = Instance.new("IntValue",leaderstats)
	Money.Name = "Money"

	local KeyLevel = Instance.new("IntValue",plr)
	KeyLevel.Name = "KeyLevel"
	
	local function moneyUpdate(updatedValue)
		moneyStore = DataStore2("money", plr)
		Money.Value = moneyStore:Get(updatedValue)
	end
	
	local function plusUpdate(updatedValue)
		plusStore = DataStore2("plus", plr)
		Plus.Value = plusStore:Get(updatedValue)
	end
	
	local function clapsUpdate(updatedValue)
		clapsStore = DataStore2("claps", plr)
		Claps.Value = clapsStore:Get(updatedValue)
	end
	
	local function keylvlUpdate(updatedValue)
		keylvlStore = DataStore2("keylvl", plr)
		KeyLevel.Value = keylvlStore:Get(updatedValue)
	end
	
	local function tclapsUpdate(updatedValue)
		tclapsStore = DataStore2("tclaps", plr)
		totalClaps.Value = tclapsStore:Get(updatedValue)
	end
	
	local function levelUpdate(updatedValue)
		levelStore = DataStore2("level", plr)
		Level.Value = levelStore:Get(updatedValue)
	end
	
	moneyUpdate(defaultValue2)
	moneyStore:OnUpdate(moneyUpdate)
	
	plusUpdate(defaultValue)
	plusStore:OnUpdate(plusUpdate)
	
	clapsUpdate(defaultValue2)
	clapsStore:OnUpdate(clapsUpdate)
	
	keylvlUpdate(defaultValue2)
	keylvlStore:OnUpdate(keylvlUpdate)
	
	tclapsUpdate(defaultValue2)
	tclapsStore:OnUpdate(tclapsUpdate)
	
	levelUpdate(defaultValue2)
	levelStore:OnUpdate(levelUpdate)	
end)

u have to make a key for all the datastores so put a string and name it master key and put it at the first of the combine arguments

1 Like

So before I list all the names I make one called “masterkey”? Do I need to put the others in a table?