Hey guys, I've been using the wiki's example of player data manager which uses datastores for my game. Recently I made a few changes here and there so that other modules in my game can use it.
But since those changes, the script doesn't work, and the only change I've made is changed the Coins to 2 other things that my game requires.
The purpose of this script is basically store player data and update player data periodically to the respective database. I might rewrite it and totally modify it to my needs one day but right now something's not right. Everything's working on server so there shouldn't be that much delay in what loads and what doesnt but I'm not sure why there's a nil value there. Can someone help me out?
This is the script I have.
local AUTOSAVE_INTERVAL = 60 local DATASTORE_RETRIES = 3 local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local PlayerStatManager = {} local sessionData = {} function PlayerStatManager:ChangeStat(player, statName, changeValue) sessionData[player][statName] = sessionData[player][statName] + changeValue player[statName].Value = sessionData[player][statName] end local function dataStoreRetry(dataStoreFunction) local tries = 0 local success = true local data = nil repeat tries = tries +1 success = pcall(function() data = dataStoreFunction() end) if not success then wait(1) end until tries == DATASTORE_RETRIES or success if not success then error('Not able to access the datastore! some of the data might not be stored!') end return success, data end local function getPlayerData(player) return dataStoreRetry(function() return playerData:GetAsync(player.UserId) end) end local function savePlayerData(player) return dataStoreRetry(function() return playerData:SetAsync(player.UserId, sessionData[player]) end) end local function setupPlayerData(player) local eggVal = Instance.new("NumberValue", player) local fireballVal = Instance.new("NumberValue", player) local success, data = getPlayerData(player) if not success then sessionData[player] = false else if not data then sessionData[player] = {Eggs = 0, Fireballs = 0} savePlayerData(player) eggVal.Name = "Eggs" eggVal.Value = 0 fireballVal.Name = "Fireballs" fireballVal.Value = 0 else sessionData[player] = data eggVal.Name = "Eggs" eggVal.Value = sessionData[player]["Eggs"] fireballVal.Name = "Fireballs" fireballVal.Value = sessionData[player]["Fireballs"] end 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
And, this is what I got for the error message
09:47:29.399 - ServerScriptService.Modules.PlayerStatManager:10: attempt to perform arithmetic on field '?' (a nil value) 09:47:29.400 - Stack Begin 09:47:29.400 - Script 'ServerScriptService.Modules.PlayerStatManager', Line 10 - method ChangeStat 09:47:29.401 - Script 'ServerScriptService.Controller.Main', Line 10 09:47:29.402 - Stack End
Why is this script not working? What can be possibly wrong with it? I don't see anything wrong personally.