DataStore2: How to access the datastore in other scripts

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.

It’s creating in Server Scripting Service

Can you send me that script? I can show you how it’s done.

local DataStore2 = require(game.ServerScriptService.DataStore2)
local MainKey = "MainDataStore"
--DataStore2.Combine(MainKey, "Stats", "OtherStats")
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)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local otherStats = Instance.new("Folder")
	otherStats.Name = "OtherStats"
	
	local stages = Instance.new("IntValue")
	stages.Name = "Stage"
	stages.Value = stageData:Get(0)
	stages.Parent = leaderstats
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Value = rebirthsData:Get(0)
	rebirths.Parent = leaderstats 
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = cashData:Get(10)
	cash.Parent = leaderstats
	
	local betaTest = Instance.new("BoolValue")
	betaTest.Name = "BetaTester"
	betaTest.Value = betaTesterData:Get(true)
	betaTest.Parent = otherStats
	
	local timePlaying = Instance.new("IntValue")
	timePlaying.Name = "Time"
	timePlaying.Value = timeData:Get(0)
	timePlaying.Parent = otherStats
	
	leaderstats.Parent = player
	otherStats.Parent = player
	
	cashData:OnUpdate(function(newDataCash)
		player.leaderstats.Cash.Value = newDataCash
	end)
end)
local DataStore2 = require(game.ServerScriptService.DataStore2)
local MainKey = "MainDataStore"
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)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local otherStats = Instance.new("Folder")
	otherStats.Name = "OtherStats"
	
	local stages = Instance.new("IntValue")
	stages.Name = "Stage"
	stages.Value = stageData:Get(0)
	stages.Parent = leaderstats
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Value = rebirthsData:Get(0)
	rebirths.Parent = leaderstats 
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = cashData:Get(10)
	cash.Parent = leaderstats
	
	local betaTest = Instance.new("BoolValue")
	betaTest.Name = "BetaTester"
	betaTest.Value = betaTesterData:Get(true)
	betaTest.Parent = otherStats
	
	local timePlaying = Instance.new("IntValue")
	timePlaying.Name = "Time"
	timePlaying.Value = timeData:Get(0)
	timePlaying.Parent = otherStats
	
	leaderstats.Parent = player
	otherStats.Parent = player
	
	cashData:OnUpdate(function(newDataCash)
		print("cashData has been updated!")
		cash.Value = newDataCash
	end)
end)
local Players = game:GetService("Players")
local DataStore2 = require(game.ServerScriptService.DataStore2)

wait(10)

for _, Player in ipairs(Players:GetPlayers()) do
	local CashData = DataStore2("Cash", Player)
	CashData:Increment(10)
end

Change the script you sent me to the one on top, and make another script for testing with the code from the second example. Tell me if it updates.

3 Likes

The second script should be in a loop?

Don’t loop it yet. This is just for testing.

You are a legend! Working! Thank you so much. How should I put it in a loop now?

Well first, remove the print from :OnUpdate, and second, you can try this.

local Players = game:GetService("Players")
local DataStore2 = require(game.ServerScriptService.DataStore2)

local WAIT_TIME = 1 -- The time between each payout.
local INCREMENT_AMOUNT = 10 -- How much they are awarded.

while true do
	wait(WAIT_TIME)
	for _, Player in ipairs(Players:GetPlayers()) do
		local CashData = DataStore2("Cash", Player)
		CashData:Increment(INCREMENT_AMOUNT)
	end
end

Seems to be working fine!
For time script, I will do the same.
For Rebirths, I will also do an event on click.
For stages, which increase when you touch them.
Should I put that function in all stages? And if so, I wouldn’t use increment but set so it’s always 100% which stage will be set.
Is there also another way to do it, not in each stage?

EDIT: I’ll actually put it in each stage.

  1. That would work.
  2. Also works.
  3. For stages, if they’re like an obby stage, do something like the script below.
  4. You can use CollectionService and the Tag Editor plugin.
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local CollectionService = game:GetService("CollectionService")
local DataStore2 = require(ServerScriptService.DataStore2)

local function TouchedFenv(StageObject)
	local Stage
	local StageNameIsStage = tonumber(StageObject.Name)
	if StageNameIsStage then
		Stage = StageNameIsStage
	else
		-- Stage is a IntValue or something.
		if StageObject:FindFirstChild("Stage") then
			Stage = StageObject.Stage.Value
		end
	end

	if type(Stage) ~= "number" then
		warn("No stage, cannot setup properly.")
		return function() end
	else
		return function(Hit)
			local Character = Hit:FindFirstAncestorOfClass("Model")
			if Character then
				local Humanoid = Character:FindFirstChildOfClass("Humanoid")
				if Humanoid and Humanoid.Health > 0 then
					local Player = Players:GetPlayerFromCharacter(Character)
					if Player then
						local StageStore = DataStore2("Stage", Player)
						StageStore:Set(Stage) -- Make sure you setup :OnUpdate!
					end
				end
			end
		end
	end
end

for _, StageObject in ipairs(CollectionService:GetTagged("Stages")) do
	StageObject.Touched:Connect(TouchedFenv(StageObject))
end

Should that be in each stage in game because I already have some script that changes stage anyway? I just need to add like 4 lines of code to change data value and will use Set instead of Increment

You can just do something like

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)

local STAGE_VALUE = 1

local function Touched(Hit)
	local Character = Hit:FindFirstAncestorOfClass("Model")
	if Character then
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if Humanoid and Humanoid.Health > 0 then
			local Player = Players:GetPlayerFromCharacter(Character)
			if Player then
				local StageStore = DataStore2("Stage", Player)
				StageStore:Set(STAGE_VALUE) -- Make sure you setup :OnUpdate!
			end
		end
	end
end

script.Parent.Touched:Connect(Touched)
1 Like

I have something similar to that. It also checks that player isn’t already above that stage.