This question is, in a way, a continuation of an earlier question I asked here. Earlier, I found out that my module script I wanted to use in a local script had yielded due to the fact that some code may not be suitable to run in a local script. The thing is, I'm not sure what can't be ran. Below is the code I wanted to use in my local script to save some data.
-- Set up table to return to any script that requires this module script local SaveFunctions = {} local playerData = game:GetService("ReplicatedStorage"):WaitForChild("DataStoreRequiringFunction"):Invoke() -- Table to hold player information for the current session local sessionData = {} -- Function that other scripts can call to change a player's stats function PlayerStatManager.ChangeStat(player, statName, value) local playerUserId = "Player_" .. player.UserId assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match") sessionData[playerUserId][statName] = value end -- Function that other scripts can call to get a player's stats function PlayerStatManager.GetStat(player, statName) local playerUserId = "Player_" .. player.UserId while wait() do if sessionData[playerUserId] then if sessionData[playerUserId][statName] then break end end end local value = sessionData[playerUserId][statName] return value end -- Function to add player to the 'sessionData' table local function setupPlayerData(player) local playerUserId = "Player_" .. player.UserId local success, data = pcall(function() return playerData:GetAsync(playerUserId) end) if success then if data then -- Data exists for this player sessionData[playerUserId] = data else -- Data store is working, but no current data for this player sessionData[playerUserId] = { Level = 1, Exp = 0 } end else warn("Cannot access data store for player!") end end -- Function to save player's data local function savePlayerData(playerUserId) if sessionData[playerUserId] then local tries = 0 local success repeat tries = tries + 1 success = pcall(function() playerData:SetAsync(playerUserId, sessionData[playerUserId]) end) if not success then wait(1) end until tries == 3 or success if not success then warn("Cannot save data for player!") end end end -- Function to save player data on exit local function saveOnExit(player) local playerUserId = "Player_" .. player.UserId savePlayerData(playerUserId) end -- Function to periodically save player data local function autoSave() while wait(30) do for playerUserId, data in pairs(sessionData) do savePlayerData(playerUserId) end end end -- Start running 'autoSave()' function in the background spawn(autoSave) -- Connect 'setupPlayerData()' function to 'PlayerAdded' event game.Players.PlayerAdded:Connect(setupPlayerData) -- Connect 'saveOnExit()' function to 'PlayerRemoving' event game.Players.PlayerRemoving:Connect(saveOnExit) return SaveFunctions
Which code should I edit? (Btw, I renamed my module script, which is why I'm using different names from the last question I asked, which I had linked to in this post)
1) You can't use DataStore on the client at all. Why are you requiring this DataStore manager from the client?
2) What is PlayerStatManager. I can seem to find it anywhere.
3) Can you explain to my what DataStoreRequiringFunction is? It seems to me that it's heavily based on DataStore which again you can't use on the client.
IIRC it errored when you tried to use DataStore on the client or something.