For several days I have been wondering why this method of saving player data doesn't work. Can anyone try this method out, and vouch that it doesn't work?
What would you need to do to test:
Create a ModuleScript
Place the ModuleScript inside the ServerStorage
Paste this code unedited, directly from the wiki inside the ModuleScript:
-- Set up table to return to any script that requires this module script
local PlayerStatManager = {}
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
-- Table to hold player information for the current session
local sessionData = {}
local AUTOSAVE_INTERVAL = 60
-- Function that other scripts can call to change a player's stats
function PlayerStatManager:ChangeStat(player, statName, value)
assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
local playerUserId = "Player_" .. player.UserId
if typeof(sessionData[playerUserId][statName]) == "number" then
sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
else
sessionData[playerUserId][statName] = value
end
end
-- Function to add player to the 'sessionData' table
local function setupPlayerData(player)
local playerUserId = "Player_" .. player.UserId
local data
local success, err = pcall(function()
playerData:UpdateAsync(playerUserId, function(playerData)
data = playerData
end)
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] = {Money=0, Experience=0}
end
else
warn("Cannot set up data for player!")
end
end
-- Function to save player's data
local function savePlayerData(playerUserId)
if sessionData[playerUserId] then
local success, err = pcall(function()
playerData:SetAsync(playerUserId, sessionData[playerUserId])
end)
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(AUTOSAVE_INTERVAL) 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 PlayerStatManager
Create a Script and place it inside of the ServerScriptService
Copy this code into the script:
local serverstorage = game:GetService("ServerStorage")
local module = require(serverstorage.ModuleScript)
game:GetService("Players").PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = 'leaderstats'
stats.Parent = player
local money = Instance.new("NumberValue")
money.Name = 'Money'
money.Parent = stats
local experience = Instance.new("NumberValue")
experience.Name = 'Experience'
experience.Parent = stats
module:ChangeStat(player, 'Money', 5)
end)
Check 'Enable Studio Access to API Services' located in the Configure this game settings.
Report back your findings.
Any help would be appreciated, thanks.

5 comments
because you need to enable api access for data stores in the place settings
Thanks for the feedback, but that's not the problem as I've already done that. Did you even read #7? Or even try it for yourself?
You never mention what is the problem. "Why doesn't this code work?" isn't a question people can answer, as not everyone has the entire roblox lua vm in their brain. At least provide details, such as if it errors, or add prints to where it saves the data and see if it prints. Same for loading it. Btw you swapped #7 and #6
If you don't know, then just read it and move on. There is no need to try to belittle me because you don't like the question that I asked. If you would've bothered to actually follow any of these steps, then you would understand my question.
TABLES ARE BROKE PEOPLE ITS 2018