I do not really understand the saving system. I tried different things to make it save a leaderstat, but it did not work. The output say error because the leaderstat did not save. At the bottom of the script, I tried PlayerStatManager:ChangeStat() because I thought it would save the player's leaderstat. What do I do to save the player's leaderstat? I tried many different things, but it did not work, so I need your help. Thanks!
local PlayerStatManager = {} local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local AUTOSAVE_INTERVAL = 64 local DATASTORE_RETRIES = 3 local sessionData = {} function PlayerStatManager:ChangeStat(player, statName) sessionData[player][statName] = 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() end return success, data end local function getPlayerData(player) return dataStoreRetry(function() return playerData:GetAsync(player.UserId) end) end local function savePlayerData(player) if sessionData[player] then return dataStoreRetry(function() return playerData:SetAsync(player.UserId, sessionData[player]) end) end end local function setupPlayerData(player) local success, data = getPlayerData(player) if not success then sessionData[player] = false else if not data then sessionData[player] = {Level = 1} savePlayerData(player) else sessionData[player] = data 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) while true do for _, player in pairs(game.Players:GetPlayers()) do PlayerStatManager:ChangeStat(player, player.leaderstats.Level.Value) end wait(64) end