How to use DataStore2 - Data Store caching and data loss prevention

So you just do this?

local DataStore2 = require(script.Parent.DataStore2)

DataStore2.Combine("DATA", "Tab")

game.Players.PlayerAdded:Connect(function(Player)
	local TableStore = DataStore2("Tab", Player)
	
	local Tab = {}
	
	TableStore:OnUpdate(function(S)
		table.insert(Tab, 1, S)
	end)
end)

No. Read the documentation, getting and setting is explained in detail.

I researched this huge topic a lot and I still couldn’t find the solution.
Using the current Studio version 440.
In my case, the error “Not running script because past shutdown deadline” occurs only while debugging, when I stop the game (which triggers DataStore2 to write the cache, I think).
I get the same message 8x.


This does not occur in the normal execution of the game (when there are no breakpoints).
How to solve this?

@Kampfkarren

If i use Get() and Set() every 0.1 sec does it cause problems cause I’ve had my data reverted a couple times when i was testing. Am i doing something wrong with my script or something else. I am currently using the latest version from the github releases v1.3.1.

Code:

	local OldStamina = PlayerStamina.Value

	PlayerStamina.Changed:Connect(function(NewStamina)
		
		local char = plr.Character or plr.CharacterAdded:Wait()
		local hum = char:WaitForChild("Humanoid")
		
		if NewStamina > OldStamina and PlayerStamina.Value < PlayerStamina.MaxValue and not char:FindFirstChild("ForceField") then
			OldStamina = NewStamina
			
			local Stats = D2("Stats",plr):Get()
			
			Stats.Stamina += 1/250
			Stats.Hunger = math.clamp(Stats.Hunger - 1/350,0,100)
			
			D2("Stats",plr):Set(Stats)

		else
			OldStamina = NewStamina
		end
		wait()
	end)
	
	while true do
		wait(0.1)
		local char = plr.Character or plr.CharacterAdded:Wait()
		local hum = char:WaitForChild("Humanoid")
			
		if not plr:FindFirstChild("StopStaminaRegen") and PlayerStamina.Value < PlayerStamina.MaxValue and Hunger.Value > 0 then
			local Multiplier = 1
			
			if plr:FindFirstChild("EnergyBoost") then
				Multiplier = 2
			end
			
			PlayerStamina.Value += (PlayerStamina.MaxValue/150) * Multiplier		
		end
	end
1 Like

Get only makes a data store call the first time. Set never does. You should be fine.

1 Like

How would I use this with soft shutdown? Whenever I attempt to saveall then do the tp, the player just disappears but never gets teleported so its just a regular shutdown
Edit: apparently there is a bind to close function in the module but never found it in the api
Edit2: apparently saveall changes the player ancestry which when i commented allowed me to actually tp properly

1 Like

Only the Coins part work for some reason Code1 doesn’t save. Also is it possible to save Bools in a leaderstats type thing?

local Players = game:GetService(“Players”)
local ServerScriptService = game:GetService(“ServerScriptService”)
local Workspace = game:GetService(“Workspace”)

local DataStore2 = require(ServerScriptService.DataStore2)
DataStore2.Combine(“DATA”, “Coins”)
DataStore2.Combine(“DATA”, “Code1”)

Players.PlayerAdded:Connect(function(player)
local CoinsStore = DataStore2(“Coins”, player)
local Code1Store = DataStore2(“Code1”, player)

local leaderstats  = Instance.new("Folder")
leaderstats.Name = "leaderstats"

local TwitterCodes = Instance.new("Folder")
TwitterCodes.Name = "TwitterCodes"

local Code1 = Instance.new("NumberValue")
Code1.Name = "Code1"
Code1.Value = Code1Store:Get(0)
Code1.Parent = leaderstats

local Coins = Instance.new("NumberValue")
Coins.Name = "Coins"
Coins.Value = CoinsStore:Get(0) -- Amount they will have by default
Coins.Parent = leaderstats

CoinsStore:OnUpdate(function(NewCoins) -- This function runs every time the datastore changes
	Coins.Value = NewCoins
end)

Code1Store:OnUpdate(function(NewCode1)
	Code1.Value = NewCode1
end)
leaderstats.Parent = player
TwitterCodes.Parent = player

end)

1 Like

instead of doing that i think you should just use

DataStore2.Combine("DATA", "Coins", "Coins1")
2 Likes

Hey, I’m currently willing to use this method and it works super fine + now that I understood how it works, I can work pretty fast with it. I have just one question: to look into players’ data, I use a plugin named DataStoreEditor (DataStore Editor - Roblox), BUT with DSS2 I don’t get what’s the actual key to find this data. I tried using the key put after DSS("Data",player) (Data is the key) but it doesn’t seem to be finding any data. Might be a really dumb question, but since I started using it a couple days ago, I’m not sure on how to find players’ data, or even if it’s possible. So, how can I find the data, possibly with that plugin?

1 Like

How to use DataStore2.ClearCache() ?

when i try to wipe all data to test everything it gives an error
Screenshot_250

Screenshot_251
its normal script

im using module inside serverscriptservice and not require(1936396537)
How can i wipe all data to test everything?

OH NEVERMIND Datastore2 is a module and i should require it lol. My bad :upside_down_face:

The key is
KEY/USER_ID

But read this to learn More and special thanks for @ MFCmaster1234

2 Likes

Awesome! Thanks for this! :smiley:

This amazing module has saved me so much time and effort with saving data, and the fact that it is so easy to use and how it just works makes it even better.
I feel like I’m writing a review or something, but I just had to express how awesome DataStore2 is…
Thank you so much for this!

I found myself unable to save data or load data from offline players because of that, also I dont quite understand why you made it so you have to use a player instance in the first place rather than just the ID of the player?

Spent some time learning how to use it and was worth it. Though I have some questions.

It’s regarding combining ‘keys’.

DataStore2.Combine("PLAYER_DATA", "Points", "Level")

Above is a line of code I made. So are the second arguments stored inside a key, which is ‘PLAYER_DATA’ in this case?

Meaning are Points and Level stored in PLAYER_DATA?

And If I were to change the name of the key that stores Points and Levels, will it reset the value associated with them?

How does it stop data throttling? ( heard from people around )

The Touched event does not return the player that touched it, it returns the part that touched it.
To get the player, you should use game.Players:GetPlayerFromCharacter(character)

game.Workspace.Path2.Touched:Connect(function(hit) -- hit is the part that touched it
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- it can return nil if hit.Parent isn't a player's character.
    if player then -- checking if it's a valid player
        local TokenData = DataStore2("Tokens", player)
        TokenData:Increment(10)
        print("Tokens increased by 10!")
        wait(10)
    end
end)

Also assuming you’re using wait(10) to make sure the event doesn’t fire too much, events don’t yield.
If you’re using wait(10) to stop the event from firing too much, use debounces instead.
The link to how to use debounces is here.

Hi, I tried it but it had the following output
22:20:17.986 - Model.MainModule:249: attempt to perform arithmetic (add) on nil and number
Did I not enable something? Because I recently transfered the game from one of my old groups to a new one.

Hello @Kampfkarren

I noticed some players joined in-game that their data doesn’t load at all.
How to solve that?

you probably missed what @RedApplePair33 said

game.Workspace.Path2.Touched:connect(function(Hit)
   local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
   if not Player then return end

   --Write your code here

end)

Please help me, I get an error with Update()