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

How would I properly save and retrieve tables from a DataStore?

Asked by 4 years ago

Here are the Variables I assigned:

local ds = game:GetService("DataStoreService")
local ds1 = ds:GetDataStore("Vrank")
local ds2 = ds:GetDataStore("ARank")

local owner
local admin = {}
local votePerms = {}

This is what I used to retrieve all the data saving into that table:

local s, e = pcall(function()
    for i, v in ipairs(ds1:GetAsync("VoteStarters",votePerms)) do
        table.insert(votePerms, #votePerms+1, v)
    end
end)

Finally, the code that is supposed to insert and save new data into the table:

if args[2] then
    if game.Players:FindFirstChild(args[2]) then
        local newRank = game.Players:FindFirstChild(args[2]).UserId
        table.insert(votePerms, #votePerms+1, newRank)
        local s, e = pcall(function()
            ds1:SetAsync("VoteStarters",votePerms)
        end)
    end
end

Thank you for taking the time to read this!

1 answer

Log in to vote
1
Answered by
Nootian 184
4 years ago
Edited 4 years ago

Use HttpService (Link 1)

This converts it into a string (Link 2)

game:GetService("HttpService").JSONEncode()

This converts it back into a table (Link 3)

game:GetService("HttpService").JSONDecode()

Link 1

https://developer.roblox.com/en-us/api-reference/class/HttpService

Link 2

https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONEncode

Link 3

https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONDecode

0
Thank you! CreationNation1 459 — 4y
Ad

Answer this question