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

can anyone help me? i've got a problem with accessing data from a module script.

Asked by 6 years ago
Edited 6 years ago

So, i am trying to make a Data Saving module and in the module itself is no error as far as i know.

ModuleScript:

local PlayerStats = {}

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreOfMe")

local AutosaveInterval = 60

local sessiondata = {}

function PlayerStats:Changing(player, statName, changeValue)
    sessiondata[player][statName] = sessiondata[player][statName] + changeValue
end

local function getplayerdata(player)
    return DataStore:GetAsync(player.UserId)
end

local function saveplayerdata(player)
    DataStore:SetAsync(player.UserId, sessiondata[player])
end

local function setupPlayerData(player)
    local data = getplayerdata(player)
    if not data then
        sessiondata[player] = {Money = 500}
        saveplayerdata(player)
    else
        sessiondata[player] = data
    end
end

local function autosave()
    while wait(AutosaveInterval) do
        for player, data in pairs(sessiondata) do
            saveplayerdata(player)
        end
    end
end

game.Players.PlayerAdded:connect(setupPlayerData)

game.Players.PlayerRemoving:connect(function(player)
    saveplayerdata(player)
    sessiondata[player] = nil
end)

spawn(autosave)

return PlayerStats

And my problem is, i can't remember how to access the "Money" variable in it. Here is how i tried accessing it.

ServerScript:

local playerStats = require(game.ReplicatedStorage.Modules.DataModule)

game.Players.PlayerAdded:connect(function(player)
    local lead = Instance.new("Folder",player)
    lead.Name = "leaderstats"

    local mon = Instance.new("NumberValue")
    mon.Name = "Money"
    mon.Value = playerStats["sessiondata"][player]["Money"]
    mon.Parent = lead
end)

The error i get is : ServerScriptService.Main:9: attempt to index field 'sessiondata' (a nil value)

I'd greatly appreciate any help!

Answer this question