I have a module script that saves player data, but it is giving me errors all over the place when trying to find player data.
local PlayerStatManager = {} local DataStoreService = game:GetService('DataStoreService') local playerData = DataStoreService:GetDataStore('PlayerData') -- Table to hold all of the player information for the current session. local 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:ChangeData(player, store, dataName, changeValue) sessionData[player][store][dataName] = sessionData[player][store][dataName] + changeValue end -- Function to retrieve player's data from the DataStore. function PlayerStatManager:getPlayerData(player, store) local key = store .. "_" .. player.UserId return playerData:GetAsync(key) end -- Function to save player's data to the DataStore. function PlayerStatManager:savePlayerData(player) local stores = {"stats", "attributes", "items"} for i = 1, #stores do local key = stores[i] .. '_' .. player.UserId playerData:SetAsync(key, sessionData[player][stores[i]]) end 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. function PlayerStatManager:setupPlayerData(player) local statsData = self:getPlayerData(player, "stats") local attributesData = self:getPlayerData(player, "attributes") local itemsData = self:getPlayerData(player, "items") if not (statsData and attributesData and itemsData) then sessionData[player] = {stats = {}, attributes = {}, items = {}} sessionData[player]["stats"] = {Coins = 0, Gold = 0, Level = 1, cpCurrent = 0, cpMax = 0, cpPerLvl = 0, cpRecovery = 0, expCurrent = 0, expNeeded = 0, hpMax = 75} sessionData[player]["attributes"] = {CON = 5, DEX = 5, END = 5, STR = 5, freePTs = 6} sessionData[player]["items"] = { } self:savePlayerData(player) elseif (statsData and attributesData and itemsData) then -- DataStores are working and we got data for this player sessionData[player]["stats"] = statsData sessionData[player]["attributes"] = attributesData sessionData[player]["itemsData"] = itemsData else print("ERROR! " .. player.UserId .. " has some data but not all!") end end -- Return the PlayerStatManager table to external scripts can access it. return PlayerStatManager
Here is an example of a call to the module script from a local script
local attributes = PlayerStatManager:getPlayerData(player, "attributes") local stats = PlayerStatManager:getPlayerData(player, "stats") local END = attributes["END"] for i = 1, END, 1 do PlayerStatManager:ChangeData(player, "stats", "hpMax", stats["hpMax"] * 0.025) end
Error Log
17:44:36.020 - Auto-Saving... Finished Loading. 17:44:53.437 - ServerStorage.PlayerStatManager:14: attempt to index field '?' (a nil value) 17:44:53.438 - Stack Begin 17:44:53.439 - Script 'ServerStorage.PlayerStatManager', Line 14 - method ChangeData 17:44:53.477 - Script 'Players.Player1.PlayerGui.interface.Frame.LocalScript', Line 46 17:44:53.478 - Stack End 17:44:53.528 - ServerStorage.PlayerStatManager:60: attempt to index field '?' (a nil value) 17:44:53.529 - Stack Begin 17:44:53.529 - Script 'ServerStorage.PlayerStatManager', Line 60 - method setupPlayerData 17:44:53.530 - Script 'Workspace.Setup', Line 6 17:44:53.530 - Stack End 17:44:58.360 - ServerStorage.PlayerStatManager:28: attempt to index field '?' (a nil value) 17:44:58.361 - Stack Begin 17:44:58.363 - Script 'ServerStorage.PlayerStatManager', Line 28 - method savePlayerData 17:44:58.363 - Script 'Workspace.Setup', Line 17 17:44:58.364 - Stack End 17:45:00.104 - ServerStorage.PlayerStatManager:28: attempt to index field '?' (a nil value) 17:45:00.105 - Stack Begin 17:45:00.105 - Script 'ServerStorage.PlayerStatManager', Line 28 - method savePlayerData 17:45:00.106 - Script 'Workspace.Setup', Line 10 17:45:00.106 - Stack End
It appears that the nested tables I created are not working, in that I could be trying to access them the wrong way. I just can't seem to figure it out after looking at it all day.