local PlayerStatManager = {} local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local sessionData = {game.Players.LocalPlayer.leaderstats.Cash.Value, game.Players.leaderstats.Subscribrers.Value} local AUTOSAVE_INTERVAL = 120 function PlayerStatManager:ChangeStat(player, statName, value) local playerUserId = "Player_"..player.UserId assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match") if typeof(sessionData[playerUserId][statName]) == "number" then sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value else sessionData[playerUserId][statName] = value end end local function setupPlayerData(player) local playerUserId = "Player_"..player.UserId local data = playerData:GetAsync(playerUserId) if data then sessionData[playerUserId] = data else sessionData[playerUserId] = {Cash=0, Subscribers=0} end end local function savePlayerData(playerUserId) if sessionData[playerUserId] then playerData:SetAsync(playerUserId, sessionData[playerUserId]) end end local function saveOnExit(player) local playerUserId = "Player_"..player.UserId savePlayerData(playerUserId) end local function autoSave() while wait(AUTOSAVE_INTERVAL) do for playerUserId, data in pairs(sessionData) do savePlayerData(playerUserId) end end end spawn(autoSave) game.Players.PlayerAdded:Connect(setupPlayerData) return PlayerStatManager
Data is not saving. I followed the ROBLOX WIKI.
Follow the instructions in the regular script. Most of your logic in the module script was fine, you just had some small issues which I fixed.
Also, instead of SetAsync() consider using UpdateAsync() to avoid dataloss.
Message me on discord if you need anything else as I added you.
module = require(game.ReplicatedStorage.ModuleScript) game.Players.PlayerAdded:Connect(function(player) --gets data from datastore / creates new data entry in DS depending on if the player has played before -- and adds player to sessionData table onenter module.setupPlayerData(player) --Whenever you want to add data do the following: module.ChangeStat(player, "Cash", 10) --prints the player's data. you might want a function called getData(player) to make this easier to read and so you can have sessionData as a private variable print(module.sessionData["Player_"..tostring(player.UserId)].Cash) --Whenever you want to update the leaderstat, do: --(localtion of the leaderstat value).Value = module.sessionData["Player_"..tostring(player.UserId)].Cash -- ^^ this is why getData() would be useful so we don't have a messy line of code like this end) --TODO: call module.saveOnExit(player) on PlayerRemoving event --this goes at the end of the script since it's an infinite loop module.autoSave()
local module = {} local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local AUTOSAVE_INTERVAL = 120 --empty table to add player data to module.sessionData = {} --function for changing a specific stat function module.ChangeStat(player, statName, value) local key = "Player_"..player.UserId module.sessionData[key][statName] = value end --save the player data local function savePlayerData(key) if module.sessionData[key] then playerData:SetAsync(key, module.sessionData[key]) end end --set up player data function module.setupPlayerData(player) local key = "Player_"..player.UserId local dataFromDS = playerData:GetAsync(key) if dataFromDS then module.sessionData[key] = dataFromDS print("loaded data from datastore") else module.sessionData[key] = {Cash=0, Subscribers=0} savePlayerData(key) print("created new data for player") end end --saves the player data on exit function module.saveOnExit(player) local key = "Player_"..player.UserId savePlayerData(key) end function module.autoSave() print("started autosave function...") while wait(AUTOSAVE_INTERVAL) do print("autosaving...") for index, playerData in pairs(module.sessionData) do savePlayerData(index) end end end return module