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

How do I update a table and save it?

Asked by 4 years ago

This has been one of my challenges with datastores and saving data. I usually have my values differentiate. Different Names.. More values. Ect. I have tried to make the script smarter and just add all of the value to the table and save the table. I have tried two methods:

Code1:

function module:SaveData(Player)
    local TblToSave = 
    {

    }
    for _, v in pairs(script[Player.Name]:GetChildren()) do
        table.insert(TblToSave, {[v.Name] = module:ViewValue(Player, v.Name)})
    end
    SavedContent:SetAsync(Player.Name, HttpService:JSONEncode(TblToSave))
end

Code2:

function module:SaveData(Player)
    local TblToSave = 
    {

    }
    for _, v in pairs(script[Player.Name]:GetChildren()) do
        TblToSave[v.Name] = module:ViewValue(Player, v.Name)
    end
    SavedContent:SetAsync(Player.Name, HttpService:JSONEncode(TblToSave))
end

None of these are working and there are no problems in the output. If you need the script that views the values ask. I'll be happy to give it.

script[Player.Name]:GetChildren() -- This is the players data. I have everything stored in server storage.

module:ViewValue(Player, v.Name) -- This is a method that just retrieves a value from the players data before it gets deleted. I use this for events so the client can see their stats

0
In order to answer this, we'll probably need more context. The second code example looks like it *should* work, but I'm not entirely sure why it isn't. I suggest placing some print() statements after each significant action to see if the code is actually populating the table properly and passing it to SetAsync Travington 0 — 4y

Answer this question