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.
