Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

DataStore Module Failure, store.stat function returning nil?

Asked by
PolyyDev 214 Moderation Voter
6 years ago

I have a Data Store module, that I writ myself, that goes like so:

local ds = game:GetService("DataStoreService")
local main = ds:GetDataStore("Main1")
local Auto_Index = 1.5 --// How many minutes until autosave
local data = {}

local store = {}

store.get = function(player)
    return main:GetAsync(player.userId)
end

store.save = function(player)
    local success,fail = pcall(function()
        main:SetAsync(player.userId,data[player])
    end)
    if success then
        return "success"
    else
        return "failed"
    end
end

store.setup = function(player)
    local current = store.get(player)

    if not current then
        data[player] = {["Coins"]=100,["Cube"]="Standard"}
        store.save(player)
    else
        data[player] = current
        store.save(player)
    end
end

store.change = function(player,stat,amount)
    data[player][stat] = data[player][stat]+amount
end

store.remove = function(player)
    data[player] = nil
end

store.autosave = function(player)
    local __index = Auto_Index*60

    spawn(function()
        local suc,err = pcall(function()
            while wait(__index) do
                store.save(player)
            end
        end)

        if not suc then
            warn("DATASTORE ERROR:",err)
        end
    end)
end

store.stats = function(player)
    return data[player]
end

store.set = function(player,stat,amount)

end

return store

Note the store.stats function

I called the store.stats function form a server script. It returned nil after previously returning the correct info.

game.Players.PlayerAdded:Connect(function(player)
    if player then
        local money = data.stats(player)        

        data.setup(player)
        data.autosave(player)

        local ls = Instance.new("Folder")
        ls.Name = "leaderstats"
        ls.Parent = player

        local m = Instance.new("NumberValue")
        m.Name = "Coins"
        m.Value = money["Coins"]
        m.Parent = ls   

        local c = Instance.new("StringValue")
        c.Value = money["Cube"]
        c.Name = "Cube"
        c.Parent = ls

        currentdata[player].Money = money["Coins"]
        currentdata[player].Cube = money["Cube"]
    end
end)

That is the function it is called from. It errors the first time it is called. Error: Attempt to index local 'money' (a nil value) I'm really confused as to why this happens, any help will be greatly appreciated.

0
Actually I realised it was called before it was setup and I changed it, it now errors on line 22 with: Attempt to index field "?" (A nil value)... help PolyyDev 214 — 6y
0
You're trying to access currentdata[player].Money when currentdata[player] hasn't been set (is nil). Adding currentdata[player] = {} before lines 22 and 23 will fix the problem. vector3_zero 1056 — 6y
0
I fixed it ages ago. PolyyDev 214 — 6y

Answer this question