Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Data not saving? Followed ROBLOX wiki

Asked by 5 years ago
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.

0
try using my blog greatneil80 2647 — 5y
0
its in the forums xd greatneil80 2647 — 5y
0
neil wat royaltoe 5144 — 5y
0
I'd rather do it this way, so I can learn where I went wrong. Anyone know what is wrong? CoreMcNugget 15 — 5y
View all comments (3 more)
0
Make sure this is in a Script (not localscript) in ServerScriptService JasonTheOwner 391 — 5y
0
Are you gettting any error messages? JasonTheOwner 391 — 5y
0
It's a module script inside server storage where they said to put it.. CoreMcNugget 15 — 5y

1 answer

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago

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.

Regular Script:

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()

Module Script

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
Ad

Answer this question