How can I use DataStore2 to increment one number in a table of numbers and strings?

I am currently making a game that uses DataStore 2 as a saving system. I have a table of strings and numbers that is saved, and I want to update/increment just one of the numbers in the table at a time, and not the strings at all. How would I achieve this?

You really can’t without forking the :Increment method.

This is how :Increment is implemented.

function DataStore:Increment(value, defaultValue)
	self:Set(self:Get(defaultValue) + value)
end

So all you need to do is change it to increment the number like so.

function DataStore:Increment(value, defaultValue)
	local t = self:Get(defaultValue)
    t.key = t.key + value
    self:Set(t)
end

However I really don’t recommend this, since you lose the abilitiy to increment numbers alone. To use the regular behaviour it requires overloading which is gonna get really messy. I discourage this even more since your use case is very niche, and I would rather just use regular data stores or just do it manually.

Overloading is generally a bad idea but that doesn’t mean you can’t still use DataStore2. You don’t need to overload, it’s much better to just add a new method. I’ve edited DataStore2 myself to add table incrementing: DataStore:IncrementKey(key, value) Even if you can’t do that, you can just manually run the table incrementing code anyway.

1 Like