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

How to save/load player data? [closed]

Asked by 5 years ago
Edited 5 years ago

I want to save what developer products a player has, stats in leaderboard, and any other data a player can have. I have access to api services in studio for my game. I wrote the script at https://developer.roblox.com/en-us/articles/Saving-Player-Data but it didnt work.

The main reason i want to do this to save stages a player has skipped with a developer product. And rebirths too.

Here is my module script in serverstorage:

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

-- Function to add player to the "sessionData" table
local function setupPlayerData(player)
    local playerUserId = "Player_" .. player.UserId
    local data = playerData:GetAsync(playerUserId)
    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] = {Stage=0, Rebirths=0}
    end
end

-- Function to save player's data
local function savePlayerData(playerUserId)
    if sessionData[playerUserId] then
        playerData:SetAsync(playerUserId, sessionData[playerUserId])
    end
end

-- Function to save player data on exit
local function saveOnExit(player)
    local playerUserId = "Player_" .. player.UserId
    savePlayerData(playerUserId)
end
-- Connect "setupPlayerData()" function to "PlayerAdded" event
game.Players.PlayerAdded:Connect(setupPlayerData)
local function autoSave()
    while wait(AUTOSAVE_INTERVAL) do
        for playerUserId, data in pairs(sessionData) do
            savePlayerData(playerUserId)
        end
    end
end 
return PlayerStatManager

its too long...i know...please help

0
Marked as non-descriptive because the actual script isn't provided (the OP needs to at least have edited it to saved whatever data they want) hiimgoodpack 2009 — 5y

Closed as Non-Descriptive by hiimgoodpack and DeveloperSolo

This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.

Why was this question closed?