DataStore2: How to access the datastore in other scripts

Yeah, but what about for another script?
If possible, can you put also variables I need?

Same thing, you just GetTable it, and you’d use the same code.

DataStore2("PlayerData", Player):Update(function(...) ... end)

2 Likes

Okay.
How would I put that in that script?

local DataStore2 = require(game.ServerScriptService.DataStore2)
local MainKey = “MainDataStore”
DataStore2.Combine(MainKey, “Stats”, “OtherStats”)

local DataStore2 = require(game.ServerScriptService.DataStore2)
local MainKey = "MainDataStore"
DataStore2.Combine(MainKey, "Stats", "OtherStats")

local addCash = function()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("leaderstats") then
			if v.leaderstats:FindFirstChild("leaderstats") then
				if v.leaderstats:FindFirstChild("Cash") then
					--[[local PlayerData = DataStore2(MainKey,v)
					PlayerData.Cash:Increment(1)
					v.leaderstats.Cash.Value = v.leaderstats.Time.Value + 1]]
					--[[DataStore2("PlayerData", v):Update(function(givesCash)
						DataStore2.Stats.Rebirths = DataStore2.Stats.Rebirths + 1
					end)]]
				end
			end
		end
	end
end

while true do
	addCash()
	wait(5)
end

So if Stats is where your Cash value is, you don’t want to update the actual Value itself. You would want to have ::OnUpdate do that for you.

local DataStore2 = require(game.ServerScriptService.DataStore2)
local MainKey = "MainDataStore"
DataStore2.Combine(MainKey, "Stats", "OtherStats")

local addCash = function()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("leaderstats") then
			if v.leaderstats:FindFirstChild("leaderstats") then
				if v.leaderstats:FindFirstChild("Cash") then
					local statsStore = DataStore2("Stats", v)
					statsStore:Update(function(currentStats)
						currentStats.Cash = currentStats.Cash + 1
						return currentStats
					end)
				end
			end
		end
	end
end

while true do
	addCash()
	wait(5)
end

And as for setting up ::OnUpdate for use with a table, you’d want to iterate through the table, check if the value exists, if it does, update the value, if it doesn’t, create it and set the value.

It doesn’t give cash for some reason :frowning:

You can use _G. or shared. These two things create different user datas for other scripts of the same class name to see.
Here’s a quick example :

There is no difference between shared and _G.

That’s looks interesting. I’ll take a look at it once I have time (tomorrow).

Not needed and not recommended at all. And as for it not updating, I’ll be home in around an hour so I can help more then.

You likely forgot to set :OnUpdate, which I did explicitly mention a few times before, but using it with a table is a bit more complicated than the docs show.

Ok, it’s really cool too. This can make data storage much easier. Lets say I have a data script and a event script.

image

Script Data looks like this :

shared.data = {}

game.Players.PlayerAdded:Connect(function(Player)
	shared.data[Player.Name] = {Coins = 0}
	script.TriggerSend:Fire(Player)
end)

Script Event looks like this :

script.Parent.TriggerSend.Event:Connect(function(Player)
	shared.data[Player.Name].Coins = shared.data[Player.Name].Coins + math.random(0,8000)
	print(Player.Name, ':', shared.data[Player.Name].Coins)
end)

Script event gets the player data by doing
shared.data[Player.Name]
and gets the coins with
shared.data[Player.Name].Coins

It also works perfectly with _G.

Script Data looks like this :

_G.data = {}

game.Players.PlayerAdded:Connect(function(Player)
	_G.data[Player.Name] = {Coins = 0}
	script.TriggerSend:Fire(Player)
end)

Script Event looks like this :

script.Parent.TriggerSend.Event:Connect(function(Player)
	_G.data[Player.Name].Coins = _G.data[Player.Name].Coins + math.random(0,8000)
	print(Player.Name, ':', _G.data[Player.Name].Coins)
end)

But keep in mind,

Is there actually a way to make it like the default datastore to save only value like every 30 seconds and on player exit?

I’m gonna probably be a little rude here, but not only is that not going to fix the problem, but it’s asinine to use shared or _G. DataStore2 already caches the data in the module. What Wizertex needs to do is set the :OnUpdate function wherever they’re setting up the stats initially.

Using the global tables is unsafe and they take longer to get anyway. There’s not a single reason to be using it, and there’s not a reason to even suggest it.

2 Likes
game.Players.PlayerRemoving:Connect(function(Player)
local coinStore = DataStore2("Coins", Player)
coinStore:Save()
-- I think
end)

DataStore2 already does that for you. That’s why you use :OnUpdate. Kampf says to not even use PlayerRemoving with DataStore2, and it’s for good reason.

You can read more here. Please read the basic simulator example as well, as it’ll be helpful to you.

Okay, so what should I do now in that Cash script?
I’m going through the documentation rn.

Changed my mind. I’ll just use normal datastore2, without tables.
However, I’ve done something, but it isn’t working.

Should give cash every 5 seconds, but doesn’t seem to be working: One piece of the script:

local cashData = DataStore2("Cash",v)
					cashData:Increment(5)
					cashData:OnUpdate(function(newDataCash)
						v.leaderstats.Cash.Value = newDataCash
					end)

Don’t set :OnUpdate inside of the loop, set it wherever you create the stat itself.

1 Like

Still doesn’t seem to be working. :frowning:
Btw. I made it like that:
(first lines:)

local dataStore = DataStore2.Combine("MainDataStore", "Stage", "Rebirths", "Cash", "BetaTester", "Time")

game.Players.PlayerAdded:Connect(function(player)	
	local stageData = DataStore2("Stage",player)
	local rebirthsData = DataStore2("Rebirths",player)
	local cashData = DataStore2("Cash",player)
	local betaTesterData = DataStore2("BetaTester",player)
	local timeData = DataStore2("Time",player)

It might just be easier if you send the main files over, and I can fix it myself.

Do you have Discord account so we can maybe communicate there if you wish?

Where are you creating the stats initially? That’s the part I need to see.