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

How would I store this player data?

Asked by 8 years ago
Edited 8 years ago

So I have module script that stores player stats, but I also need to store attributes and items in a different table, under each player. Module script is below.

-module script

local PlayerStatManager = {}


local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')

-- Table to hold all of the player information for the current session.
local sessionData = {} --Holds players with a table of stats attached

-- Function the other scripts in our game can call to change a player's stats. This
-- function is stored in the returned table so external scripts can use it.
function PlayerStatManager:ChangeStat(player, dataName, changeValue)
    sessionData[player][dataName] = sessionData[player][dataName] + changeValue
end

-- Function to retrieve player's data from the DataStore.
function PlayerStatManager:getPlayerData(player)
    return playerData:GetAsync(player.UserId)
end

-- Function to save player's data to the DataStore.
function PlayerStatManager:savePlayerData(player)
    playerData:SetAsync(player.UserId, sessionData[player])
end

-- Function to add player to the sessionData table. First check if the player has
-- data in the DataStore. If so, we'll use that. If not, we'll add the player to
-- the DataStore.


function PlayerStatManager:setupPlayerData(player)
    local data = self:getPlayerData(player) --how would I call getPlayerData if setupPlayerData is called in another server script?
    if not data then
        sessionData[player] = {Coins = 0, Gold = 0, Level = 1, cpCurrent = 0, 
            cpMax = 0, cpPerLvl = 0, cpRecovery = 0, expCurrent = 0, 
            expNeeded = 0, hpMax = 75, hpPerLvl = 0.15}

        self:savePlayerData(player) --creates a new set a data under a player if the data doesn't exist(meaning this player has never played the game before
    else
        -- DataStores are working and we got data for this player
        sessionData[player] = data
    end
end

-- Function to run in the background to periodically save player's data.

-- Start running autosave function in the background.


-- Return the PlayerStatManager table to external scripts can access it.
return PlayerStatManager

The only way to store things like items and attributes in the above script would be in the hierarchy example below.

--[[


                      >Stats
sessionData>Players>Attributes
                      >Items>Item stats(these are random and not constant for each player...)


]]--

The script is giving me an error when I try do this though, saying it is "deprecated", which I can understand it being cumbersome and inefficient. I'm just really confused on how else to do this. Even if I made a new module script for each set of data, items still need tables under them...

0
Can you tell us what is deprecated and where? I don't wanna waste DataStore space, after all ROBLOX needs that space on their servers. funzrey 58 — 8y
0
when I try make stats and attributes two seperate tables under player, it underlines stats as deprecated dragonkeeper467 453 — 8y

Answer this question