So I have this module, and whenever I try to call this function:
(around line 23)
-- Read data function
function PlayerStatManager:GetData(player, statName)
warn("DATA : ",unpack(sessionData or {}))
return sessionData[player.Name][statName]
end
it gives me this error:
SaveDataModule:23: attempt to index field ‘?’ (a nil value)
This is the entire module. It is a modified version of the one on the official Saving Player Data Roblox wiki page
-- Setup table that we will return to scripts that require the ModuleScript.
PlayerStatManager = {}
-- Create variable for the DataStore.
DataStoreService = game:GetService('DataStoreService')
playerData = DataStoreService:GetDataStore('PlayerData2'.. game.PlaceId )
-- Create variable to configure how often the game autosaves the player data.
AUTOSAVE_INTERVAL = 60
-- Table to hold all of the player information for the current session.
sessionData = {}
-- Function the other scripts in our game can call to change a player's stats. This
-- function is stored in the returned table so external scripts can use it.
function PlayerStatManager:ChangeStat(player, statName, changeValue)
sessionData[player.Name][statName] = sessionData[player.Name][statName] + changeValue
end
-- Read data function
function PlayerStatManager:GetData(player, statName)
warn("DATA : ",unpack(sessionData or {}))
return sessionData[player.Name][statName]
end
-- Function to retrieve player's data from the DataStore.
local function getPlayerData(player)
return playerData:GetAsync(player.UserId)
end
-- Function to save player's data to the DataStore.
local function savePlayerData(player)
playerData:SetAsync(player.UserId, sessionData[player.Name])
end
-- Function to add player to the sessionData table. First check if the player has
-- data in the DataStore. If so, we'll use that. If not, we'll add the player to
-- the DataStore.
local function setupPlayerData(player)
local data = getPlayerData(player)
if not data then
-- DataStores are working, but no data for this player
sessionData[player.Name] = {Money = 0,
MaxHealth = 100,
MaxShield = 100,
["tools"] = {
-- {"Sword","Classic sword",{}},
{"Ax","Classic ax",{"Speedy","Strong"},{2,3}}
},
["items"] = {
{"Wood",5,"The stuff of trees"},
{"Fish",3,"a limbless animal with gills and fins"}
}
}
savePlayerData(player)
else
-- DataStores are working and we got data for this player
sessionData[player.Name] = data
end
end
-- Function to run in the background to periodically save player's data.
local function autosave()
while wait(AUTOSAVE_INTERVAL) do
for player, data in pairs(sessionData) do
savePlayerData(player)
end
end
end
-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)
-- Call savePlayerData on PlayerRemoving to save player data when they leave.
-- Also delete the player from the sessionData, as the player isn't in-game anymore.
game.Players.PlayerRemoving:connect(function(player)
savePlayerData(player)
sessionData[player.Name] = nil
end)
-- Start running autosave function in the background.
spawn(autosave)
-- Return the PlayerStatManager table to external scripts can access it.
return PlayerStatManager
-- PlayerStatManager:ChangeStat(player, statName, changeValue)
-- PlayerStatManager:GetData(player, statName)
-- https://www.roblox.com/Thumbs/Asset.ashx?width=110&height=110&assetId=1818
what is annoying me is the fact that the script errors because it thinks sessionData is undefined when I try to call it in
return sessionData[player.Name][statName]
and sessionData is clearly defined a few lines above
I’ve been having this problem for a while now.
Originally, I posted this on the script helpers forum
But that post seems to be dead and inactive, so I thought I would post on this new fancy schmancy official Roblox developers forum.
Also, I would like to point out that
- warn is a built-in Roblox function, similar to print
- Unpack is a built-in Roblox function as well (it is the same as doing return list[i], list[i+1], ···, list[j])
- There is nothing wrong with the way I am concatenating
- This will still error without the warn. The problem is with sessionData being a “nil value”
This is for my game, Sorcery