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

How would I save a table using DataStore2?

Asked by 4 years ago

So I'm trying to save the player's data using DS2 but I'm not sure dow I would do this. I'm fairly new to DS2.

1 answer

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

To load: Create a new Datastore2, which we'll store in DS. Call :GetTable() on DS, with a table of defaults inside, and do whatever you want with the result. - If any key inside of defaults is not found in the data receiver from :GetTable(), it'll be added to the data.

To set: Gather all the data that you want to set. Create a new Datastore2, which we'll store in DS once again. Call DS:Set() with the table containing player data as argument.

To save: Create a new Datastore2, which we'll store in DS Then, call DS:Save()

Code sample:

local DataStore2 = require(1936396537)
local Defaults = {} -- Place all your defaults in here

function Load(Player)
    local DS = DataStore2('PlayerData',Player)
    return DS:GetTable(Defaults)
end

function Set(Player)
    local DS = DataStore2('PlayerData',Player)
    local Data = {} -- Replace this with whatever you use to get your data
    DS:Set(Data)
end

function Save(Player)
    local DS = DataStore2('PlayerData',Player)
    DS:Save()
end

Please note: Usually, you don't call a save function using Datastore2, instead, you set the data, and Datastore2 will save it at the end of the session. You should only call a save function if something important needs to be saved

0
Tysm! Helped me out a lot. JJBIoxxer 50 — 4y
Ad

Answer this question