Saving tables in datastore2?

I’d like to save a table of character part names and stuff in datastore2.
I’m honestly clueless of how to properly save and load the tables. I’m rather new to datastore2.

Any suggestions or examples are highly appreciated, thanks for reading!

6 Likes

You save tables the same way you save any other data. And loading as well. Tables are no different to any other types that can be saved (e.g strings)

1 Like
local DataStore2 = require(script.Parent:WaitForChild("DataStore2"))
DataStore2.Combine("Table")

local DefaultTable = {Value = 0}

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerTableValue = {Value = 0}
	local TableStorage = DataStore2("Table", Player)
	local function Update(NewTable)
		PlayerTableValue = NewTable
		print(PlayerTableValue.Value)
	end
	local Table = Update(TableStorage:GetTable(DefaultTable))
	TableStorage:OnUpdate(Update)
	if Table then
		print(Table.Value)
	end
end)

I tried this and it did not work.

You don’t need to use :GetTable(), you can just save it exactly the same as anything else.
Like how you would save a NumberValue with :Get() and :Set(), you use the same with a table. You can use :GetTable(), but I see no need.

1 Like
		local CharTable = {
			HairColor = nil,
			SkinColor = "Test",
		}
		local function OnUpdate(NewTable)
			CharTable = NewTable
		end
		local CharacterStorage = Ds2("Character", Player)
		OnUpdate(CharacterStorage:Get({}))
		CharacterStorage:OnUpdate(OnUpdate)

I used this, but it didn’t work.

2 things!

First, when you use :Get() with a table, you don’t need to have {} inside, you can just leave it blank.
Secondly, the way you save could over-save, instead of saving it when you change it, save it when the player leaves, when the server shutsdown with game:BindToClose(), and every 100-200 seconds. Like:

local CharTable = {
    HairColor = nil,
    SkinColor = "Test"
}

local CharacterStorage = Ds2("Character", Player)

CharTable = CharacterStorage:Get() -- this loads the data

game.Players.PlayerRemoving:Connect(function()
    CharacterStorage:Set(CharTable) -- this saves the data
end)

-- not in Studio so I'm not gonna do BindToClose, but its pretty easy to look up

while true do
    wait(200)
    CharacterStorage:Set(CharTable)
end

You use :Set() to save, and :Get() to load.

15 Likes

:GetTable seems to work better in this case.

1 Like

I’ve never used datastoring tables in that way, but I see what you mean by :GetTable() does seem to work better in this case.

3 Likes

How do you detect when the table has changed so we set the new data instead of having to set when player is removing or every specific time?

Whatever you use to change the table, can send a remote to save.

However, with Datastore2 the limit on how often you can save is pretty much removed since it only publishes to Datastore on leave. So you can set a loop to save every 1 second and it wont time out.

If it really is important to you to save when the table changes, remember that the only time the table changes is when you tell it to change. Saving whenever you make a change will be the same as detecting when the table changes.

1 Like

Thanks. How can we modify the data in the table from other scripts? Could you show me an example?
also, this does not require combining right? image

The error from the module said the dataStore name has to be a string which it is as shown.

don’t worry about it, I fixed it. I’m just really new to datastore2

1 Like

Its much better to use :Combine() just because it makes your data smaller, which is better for datastore rates.

You would need to use a BindableEvent or BindableFunction (if you want to return something) that will change stuff in the table.

Bindable’s can only be activated and :Connect()'d on the server, so they can’t be exploited like RemoteEvents can be.

1 Like

Yep, that’s what I did and would you take a look at this post

It’s fine if you are not able to fix this. ButI just wanna know if I have to combine all of each keys for all of the things in the tables?

No, you just need 1 key for a table.

It seems to not be saving the tables content. Do you know why? It does say it’s saving the “key” though. Weird.

I fixed it. Don’t worry about it. The datastorename and the keystring must be the same string.