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

Table inside of a ModuleScript is returning nil when it shouldn't be?

Asked by
Troidit 253 Moderation Voter
7 years ago
Edited 7 years ago

I'm using a ModuleScript to create an index that stores the amount of points a player has, and saves it with DataStores when they leave (as well as during an autosave). With the help of the Roblox Wiki I was able to script what looks to me like it should work. ModuleScript: [EDITED]

local PlayerStatManager = {}

local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PhobiaSave')

local AUTOSAVE_INTERVAL = 90

local sessionData = {}

function PlayerStatManager:ChangeStat(player, statName, changeValue)
    sessionData[player][statName] = sessionData[player][statName] + changeValue
end

function PlayerStatManager:RetrieveData(player) --Function I'm operating from the regular script
    return sessionData[player]
end

local function getPlayerData(player)
    return playerData:GetAsync(player.UserId)
end


local function savePlayerData(player)
    playerData:SetAsync(player.UserId, sessionData[player])
end

local function setupPlayerData(player) --Doesn't operate this function (BREAK HERE)
    local data = getPlayerData(player)
    if not data then
        sessionData[player] = {Intelligence = 0}
        savePlayerData(player)
    else
        sessionData[player] = data
    end
end

local function autosave()
    while wait(AUTOSAVE_INTERVAL) 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 PlayerStatManager

Server Script: [EDITED]

Storage = require(script.StatStorage)
game:GetService("Players").PlayerAdded:Connect(player)
    print(Storage:RetrieveData(Player)) --returns nil
end

When I run in test mode and insert a break where I commented above (BREAK HERE) inside the ModuleScript, it doesn't break showing that it's not running the function when required to on PlayerAdded? I'm not sure if that's related to the problem of my Server Script trying to get something that returns nil from the ModuleScript.

1 answer

Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

You put Player, not player. Also, you should be using the two together in unison. You have two PlayerAdded functions going on. You should call it from the script to the module, else they're probably going to fire in different orders and that's not going to work out well online. Ie.. index the player when they join via script calling the setupPlayerData function in the module, return data.

local module = require(script:WaitForChild('ModuleScript'))

game.Players.PlayerAdded:connect(function(player)
    local data = module:setupPlayerData(player) 
    print(data.Points)
end)

game.Players.PlayerRemoving:connect(function(player)
    module:player_left(player)
end)

local PlayerStatManager = {}

local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PhobiaSave')

local AUTOSAVE_INTERVAL = 90

local sessionData = {}

function PlayerStatManager:ChangeStat(player, statName, changeValue)
    sessionData[player][statName] = sessionData[player][statName] + changeValue
end

function PlayerStatManager:getPlayerData(player)
    return playerData:GetAsync(player.UserId)
end

local function savePlayerData(player)
    playerData:SetAsync(player.UserId, sessionData[player])
end

local function autosave()
    while wait(AUTOSAVE_INTERVAL) do
        for player, data in pairs(sessionData) do
            savePlayerData(player)
        end
    end
end

function PlayerStatManager:setupPlayerData(player) 
    local data = PlayerStatManager:getPlayerData(player)
    if not data then
        sessionData[player] = {Points = 0}
        savePlayerData(player)
    else
        sessionData[player] = data
    end
    return sessionData[player]
end

function PlayerStatManager:player_left(player)
    savePlayerData(player)
    sessionData[player] = nil
end

spawn(function() autosave() end)

return PlayerStatManager
Ad

Answer this question