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

ok in whichcase how would i increment a specific value in the datastore? just Datastore:Increment(1) how does that find the value in the store?

1 Like

:Increment only works if the data value is a number. That’s why as said before:

3 Likes

i understand that it needs to be a number… so what i am to understand is that if the datastore is a table then specific values inside that might be a number cant be incremented with that function. Increment the value outside of it,and reimplement the new table in the store.

2 Likes

Yes. How would you increment the table’s value normally? Whatever it is, do that, then call :Set on the table–DataStore2 doesn’t care how you set the variable.

2 Likes

I dont need to put anything in the parentheses? or do i put the new table in the Datastore:Set(NewTable)

1 Like

Again, just set it normally. Tables aren’t anything special.

2 Likes

I’m sorry i feel so dumb not totally understanding how the datastore indexes each name with the value attached into the 1 store. I feel like such a pest rn XD.

2 Likes

Again, don’t think of tables as anything different. When you call :Get(), it’ll return the table. Do whatever you want with the table. Then call :Set. I’m not sure what else to say.

local things = thingStore:Get()
things.Something = things.Something + 1
thingStore:Set(things)
4 Likes

that is very helpful thankyou :slight_smile:

again i apologize for sounding like a total doofus XD, i guess we all have things we just cant wrap our heads around. You are very patient thank you.

4 Likes

I’m sorry just one more thing: This is what im doing to get the values into the datastore to set the values from the default table, into the datastore…

local SpellsInStore = SpellsStore:Get()
local function UpdateSpellStore(UpdateVal)
	for statname,statval in pairs(SpellDefaultTab) do

		SpellData[statname].Value = SpellsStore:Get(statname.UpdateVal)
		
	end
end
for i,v in pairs(SpellDefaultTab) do
	UpdateSpellStore(v)
end

SpellsStore:OnUpdate(UpdateSpellStore)

end)

basically i want the statname to equal the updateval in the datastore.

2 Likes

Ahh it should be GetTable:({statname = UpdateVal})

    local function UpdateSpellStore(UpdateVal)
	for statname,statval in pairs(SpellDefaultTab) do
		SpellsStore:GetTable({statname = UpdateVal})
		local SpellsInStore = SpellsStore:Get()
		SpellData:FindFirstChild(statname).Value = SpellsInStore[statname].Value
     end
 end

this is not working :sob:

2 Likes

I’m not sure if the move for a package port of DataStore2 was ever completed after the initial attempt, but are there plans to try again? The Roblox model is pretty much a fork if you aren’t requiring it by its AssetId and the same goes for copying the raw source from the GitHub repo. I think creating this as a package would be extremely valuable for usage.

3 Likes

I’ve tried a few times and couldn’t get it working–I can try again sometime soon.

4 Likes

Hello, I’m having an issue when trying to use Datastore2. Im trying to save a table of stats/currencies, however I’m getting an error in my script. I’m not sure what I’m doing wrong.

local DataModule = require(1936396537)
local DefaultCurrency = {
		['Speed'] = 6,
		['Steps'] = 0,
		}

game.Players.PlayerAdded:Connect(function(player)
	DataModule.Combine('Data','Currency','Gates')
	
	local Folder = Instance.new('Folder')
	Folder.Parent = player
	Folder.Name = "PlayerStats"
	
	local Speed = Instance.new('IntValue')
	Speed.Parent = Folder
	Speed.Name = "Speed"
	
	local Steps = Instance.new('IntValue')
	Steps.Parent = Folder
	Steps.Name = "Steps"
	
	
	local CurrencyData = DataModule('Currency', player)
	local GateData = DataModule('Gates', player)
	
	local function UpdateCurrency(default)
		Speed.Value = CurrencyData:Get(default).Speed
		Steps.Value = CurrencyData:Get(default).Steps
	end
	
	UpdateCurrency(DefaultCurrency)
	
	
	CurrencyData:OnUpdate(UpdateCurrency)
	
end)

game.Workspace.TestPart.Touched:Connect(function(o)
	if o.Parent:FindFirstChild('Humanoid') then
		local player = game.Players:GetPlayerFromCharacter(o.Parent)
		if player then
	
			local CurrencyStore = DataModule('Currency', player)
			CurrencyStore:Get(DefaultCurrency).Steps:Increment(5,0) -- its saying that this is a nil value
			
		end
	end
 end)

In the output it says ‘failed to index nil value’ at the part of the script where I put the comment. It seems that the ‘Steps’ aren’t in the currency data. Am I doing something wrong or forgetting something? I will appreciate all help, thanks. :smiley:

2 Likes

Because that’s not how you call :Increment. :Increment is a method on the data store, not on whatever Steps is.

Also, call Combine outside of any connections–it only needs to run once (and before everything else).

1 Like

When I try this it says ‘unable to perform arithmetic on nil value’:

CurrencyStore:Get(DefaultCurrency).Steps = CurrencyStore:Get(DefaultCurrency).Steps + 5

That’s not going to work either. Get the table. Manipulate it. Then call Set. If you’re still getting an error, that means your default value or the data doesn’t have the Steps key.

1 Like

This seems to be more of a basic misunderstanding of how to code in Lua rather than an issue with the library, and as such, probably should be posted elsewhere.

2 Likes

Would DataStore2 be used more as a “backup” for data rather than the main data store? I remember someone (ForeverHD?) saying something about using it as a backup.

DataStore2 can be used as your main DataStore, but the difference between them is that DataStore2 does not overwrite the previous data, but saves the data on a different scope, allowing you to be able to restore data from past sessions.

2 Likes