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

Can someone give me a better understanding of Saving Player Data using tables?

Asked by 5 years ago

Hello, I want to use the following code to save a table.

-- 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 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] = {Money=0, Codes=[]}
        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 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

I have search everywhere trying to find out how to use it. The following link tells me how to write it but doesnt tell me how to read data from it. https://developer.roblox.com/articles/Saving-Player-Data Just say i want to create a intvalue how do i read what the intvalue says from the following script. If you can explain it that would be much appreciated or give links to where i can i have searched and nothing to be found. Thanks.

0
Im not really good at Data saving things. Sorry xXnutcrakerXx -10 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Roblox wiki's way of saving data sucks, use mine instead because it is good.

local table = {} -- in here we will insert names, then for the names we will give it values.
local ds = game:GetService("DataStoreService"):GetDataStore("nothing")
game.Players.PlayerAdded:Connect(function(p) -- get the player
    if ds:GetAsync(p.UserId) then
        table[p.Name] = ds:GetAsync(p.UserId) -- your "table" should be "person" = {} now..
        -- we put the data in the table to conserve its state so we dont erase data.
        -- now you can fire the data, do whatever u want with what is in the table..
    end
end)
game.Players.PlayerRemoving:Connect(function(p)
    local newSave = {}
    for _, v in pairs(table[p.Name]) do
        table.insert(newSave,v)
    end
    ds:SetAsync(p,newSave)
end)

spawn(function()
    while true do wait()
        --table[player][2] == "2323"
    end
end)

game:BindToClose(function()
    for _, v in pairs(table) do
        ds:SetAsync(game.Players[tostring(v)].UserId,table[v])
    end
end)

I didnt test this but this is how i'd save data. If you need my data saving module, just ask... Line 18-20 if u wanna check if the data saving works, its an internal saver, so you have to convert the values back to ints try using print() for most of it..

Ad

Answer this question