Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I save a table using DataStore2?

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

✓ Accepted answer

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

1 comment

0

Tysm! Helped me out a lot.

This discussion is preserved as it was. New answers, comments, and votes are closed.