DataStore2 Table Nil, require assistance

Hello, I am currently trying to make a DataStore2 saving system using tables such as the Currency and Levels one. I have not been able to get it to work due to the table returning nil whenever I try to get the value in my Construction script on a tool.
I have tried looking at many different tutorials and rewritten it about 5 times but just can’t seem to work. Could someone tell me what I’m doing wrong?

--DATASTORE2 SCRIPT IN SCRIPTSERVICE
 
local DataStore2 = require(game:GetService("ServerScriptService").DataStore2)
 
local MainKey = "MainKey"
DataStore2.Combine(MainKey, "Currency", "Levels")
 
local defaultCoins = 0
local defaultTokens = 0
local defaultLevel = 0
 
local function SetDataTable()
    local UserData = {
        Currency = {
            ["Coins"] = 0,
            ["Tokens"] = 0,
        }, 
        Levels = {
            ["Mininglvl"] = 1,
            ["MiningXP"] = 0,
            ["Woodcuttinglvl"] = 1,
            ["WoodcuttingXP"] = 0,
        },
       
    }
    return UserData
end
 
game.Players.PlayerAdded:Connect(function(ply)
    --local coinsDataStore = DataStore2("coins",ply)
    --local miningData = DataStore2("mininglevel",ply)
    local userData = DataStore2(MainKey,ply):Get(SetDataTable())
 
 
    local leaderstats = Instance.new("Folder"); leaderstats.Name = "leaderstats"
    local coins = Instance.new("IntValue"); coins.Name = "Coins"
    local tokens = Instance.new("IntValue"); tokens.Name = "Tokens"
    local mininglvl = Instance.new("IntValue"); mininglvl.Name = "MiningLevel"
    local woodcuttinglvl = Instance.new("IntValue"); woodcuttinglvl.Name = "WoodcuttingLevel"
    leaderstats.Parent = ply
    coins.Parent = leaderstats
    tokens.Parent = leaderstats
    mininglvl.Parent = leaderstats
    woodcuttinglvl.Parent = leaderstats
   
    local CurrencyData = DataStore2("Currency", ply):Get()
    local LevelData = DataStore2("Levels", ply)
   
    local function UpdateAllStats(UpdatedStats)
        coins.Value = DataStore2(MainKey, ply):Get(UpdatedStats)["Currency"]["Coins"] -- Sets value you've made to correspond with data table
    end
 
 
    while wait(3) do
        --print(CurrencyData.Coins)
        --print(CurrencyData)
    end
end)
 
--CONSTRUCTION SCRIPT IN A TOOL
local debounce = true
local cooldown = 1
local ScriptServer = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
local Retrival = require(ScriptServer.DataStore2)
 
local MainKey = "Keyword"
 
script.Parent.SmackHammer.OnServerEvent:Connect(function(ply)
   
    if debounce then
        print(Retrival("Currency",ply).Coins)
        debounce = false
        wait(cooldown)
        debounce = true
    end
   
end)

The DataStore object doesn’t have .Coins (and is therefore nil), I believe you want to :Get() the data and then index Coins:

local DataStore = Retrival("Currency", ply)
local Coins = DataStore:Get(--[[ Put default value here ]]).Coins
print(Coins)

FYI: When working with tables in DS2, it’s prefered to use :GetTable()

1 Like