Help with datastore 2

Relevant API

DataStore2 DataStore2(dataStoreName, player)

Example usage: local coinStore = DataStore2(“coins”, player)

This is what the module returns when you require it. You usually use this on PlayerAdded. Note that the data store name will not be what the name of the data store (because of how the saving method works).

Variant DataStore2:Get(defaultValue=nil, dontAttemptGet=false)

Example usage: coinStore:Get(0)

If there is no cached value, it will attempt to get the value in the data store. Otherwise, it’ll return the cached value. If whatever value it gets is nil, it will return the defaultValue passed. If dontAttemptGet is true, then it will return nil if there is no cached value.

Variant DataStore2:GetTable(defaultValue=nil)

Example usage: coinStore:GetTable({ coins = 0, gems = 0 })

Identical to :Get() only instead of only using the default value if the table is nil, GetTable will check the value from Get (which must be a table) to check if it has every key provided in the default value. If it does not, it will add the key.

So for 1 this probably shouldn’t exist

should be

local UserStats = DataStore2("UserStats", plr):GetTable(setDataTable())

setDataTable() needs to return your userData
as @Hexlinee mentioned or just not be in a function.
So something like:

local defaultStats = {}
defaultStats.XP = 0
--then ...
local UserStats = DataStore2("UserStats", plr):GetTable(defaultStats) 
2 Likes