I want to save tables in Data Store!

I want to save tables in Data Store!
For instance, I want Items within a player’s Inventory will be saved in the data store by using a table If the player left.

I know How I make script running when the player leaves and saving her Inventory.

But I don’t know how to save tables in Data store.

Could you explain the way me?

(I didn’t make my English perfect. Sorry.)

Just save tables like any other data

local tab = {Cash = 4, Coins = 10};
DataStore:SetAsync(key, tab)
-- of course this is just an example of how you would save data and not what I'm telling you to literally do.

UpdateAsync considers the older value before saving, it’s dangerous to use Set() at times where you need the previous value.

cc @ArtFoundation

Use datastore2
Use datastore2

Thank you for your help:D
Your help makes me feel grateful.

Might wanna avoid SetAsync and use UpdateAsync generally if you can. Here’s why.

@roasted_shrimps for future reference, you can save almost anything as long as it’s not something userdata like objects. So tables, dictionaries, and arrays will work when saving. Strings, numbers, whatever like that.

Just for clarity,
@roasted_shrimps, there’s a process called serialization, you can serialize data that is not in the right format to be saved itself (vector3 values for example), technically not just anything can be saved to a DataStore.

For reference, don’t JSONEncode tables when saving them to a DataStore thinking they can’t be saved themselves, anything that needs to be saved is turned into a string format itself (only strings can be saved to the DataStore), so JSONEncoding it would just increase the amount of characters you’re saving.

Edit: didn’t mean to reply to you

1 Like

I would like to also clarify that what I mean is just based on normal datastore saving so any serialization is possible just that you’re well technically saving still tables, dictionaries, arrays, strings, numbers, etc that is normally accepted but in replacement of something that can’t be saved or loaded normally. Serialization is definitely useful in saving object data.

EDIT: Oops guess you were replying to the original poster, this reply is now useless

cc @ArtFoundation, @XxELECTROFUSIONxX using Set is fine. Mass-replacing Set with Update ruins the whole point of actually updating the value. You can use Set just fine if you need to force data or if you really just don’t care about the old value. You use Update when you actually need the previous data

3 Likes

Yes, you’re correct, though I would say it’s a good habit to know to use Update as well since I wouldn’t want to encourage always using Set for every use case.

Set is fine for every use case. Only use Update for updating existing data. I wouldn’t say Set is a bad habit like you’re suggesting

1 Like

Hey I’ve got a question.

DataStore:SetAsync(key, tab)

Does it save the value in the table that has changed or the whole value?

For example I made a table for Level and Points.

So if Level changes, will Points change as well? ( when using tables )

Calling DataStore:SetAsync(key, v) just overrides whatever was previously saved to the key, doesn’t fill in missing or changed indexes.

you will need to change the value in the table when the value is changed

something.Changed:Connect(function()
      thetable.something = something.Value
end)
1 Like