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

How do I fit more data into this script by the wiki?

Asked by 6 years ago
Edited 6 years ago

I want to make my data stores more safe, so I've wanted to use the wiki's version. But the issue is

the wiki does this:

sessionData[player] = 1000

but I want to add more to it, so I've tried to do this:

sessionData[player].Money = 1000

is there any way I can expand this data store or any way I can save it like this:

sessionData[player].Money = 1000

instead of doing it like this:

sessionData[player] = 1000

so I can add more things to the table

I know how to do the basic data stores but this one kind of confuses me please help;-;

this is the full script from the wiki (edited by me) btw:

local rs = game:GetService('ReplicatedStorage')
local house = rs.house
local price = house.price
local filteringname = rs.filtering.filtername
local suc = rs.filtering.success
local resetavatar = rs.avatareditor.resetavatar
local ava = rs:WaitForChild("avatareditor")
local makeavatar = ava:waitForChild("makeavatar")

-- Create variable for the DataStore.
local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')

-- Create variable to configure how often the game autosaves the player data.


-- Number of times we can retry accessing a DataStore before we give up and create
-- an error.
local DATASTORE_RETRIES = 3

-- Table to hold all of the player information for the current session.
local sessionData = {}
-- 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 to retry the passed in function several times. If the passed in function
-- is unable to be run then this function returns false and creates an error.
local function dataStoreRetry(dataStoreFunction)
    local tries = 0 
    local success = true
    local data = nil
    repeat
        tries = tries + 1
        success = pcall(function() data = dataStoreFunction() end)
        if not success then wait(1) end
    until tries == DATASTORE_RETRIES or success
    if not success then
        error('Could not access DataStore! Warn players that their data might not get saved!')
    end
    return success, data
end

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

-- Function to save player's data to the DataStore.
local function savePlayerData(player)
    if sessionData[player] then
        return dataStoreRetry(function()
            return playerData:SetAsync(player.UserId, sessionData[player])
        end)
    end
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.

local function setupPlayerData(player)
    local success, data = getPlayerData(player)
    if not success then
        -- Could not access DataStore, set session data for player to false.
        sessionData[player] = false
    else

        if not data then
            print("SETTING IT UP")

            -- DataStores are working, but no data for this player


        sessionData[player].Money = 1000
        sessionData[player].hasa = true
        sessionData[player].normalavatar = true

            savePlayerData(player)
            local number = sessionData[player].Money
             price:FireClient(player,number)
        else
            print("LOADING IT UP")
            -- DataStores are working and we got data for this player
            sessionData[player] = data
            local number = sessionData[player].Money
             price:FireClient(player,number)
        end
    end 
end



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


-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)

-- Call savePlayerData on PlayerRemoving to save player data when they leave.
-- Also delete the player from the sessionData, as the player isn't in-game anymore.
game.Players.PlayerRemoving:connect(function(player)
    savePlayerData(player)
    sessionData[player] = nil
end)


0
Keep in mind that the original is in a module script, so I edited for it to be in a normal script, if you're wondering why I did this, it's because I'm going to optimize it (aka put all the data stuff in one script) Unbunn_makes 48 — 6y

1 answer

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

You want to write it like this:

sessionData[player] = { Money = 1000, hasa = true, normalavatar = true }

Or this:

sessionData[player] = {}
sessionData[player].Money = 1000
sessionData[player].hasa = true
sessionData[player].normalavatar = true
0
but I want to put tables inside of it.. Unbunn_makes 48 — 6y
0
wait, I tried that and this error happens: ServerScriptService.Script:75: attempt to index field '?' (a nil value) Unbunn_makes 48 — 6y
0
You can put tables. For instance sessionData[player] = { PlayerInfo = { Level = 4, Gold = 500 }}. As for the error, which way did you write it? If it was the second you need to make sure you have the line sessionData[player] = {} before adding the other fields. vector3_zero 1056 — 6y
0
THANK YOU SO MUCH Unbunn_makes 48 — 6y
0
You're welcome. You can even have multiple tables if you needed, like: sessionData[player] = { PlayerInfo = { Level = 4, Gold = 500 }, PlayerInventory = { Head = "Bucket", Chest = "Steel"}}, or even sessionData[player] = { PlayerInfo = { Level = 4, Gold = 500, Inventory = { Head = "Bucket", Chest = "Steel"}}} vector3_zero 1056 — 6y
Ad

Answer this question