I have been using DataStore now for some time. However, I get to the point where I can only save a players money value and not their xp.
How do I go about saving a players money and their xp in a datastore at once when they leave and join
I have done the following where I specify data to the value example. local data = Player.leaderstats.Money.Value. However it only works with 1 and not 2. I even duplicated em one xp and one money.
My data stores work, however, I dont want to just be able to save 1 thing of the entire game you know…
I know how to make the whole datastore. I am just very confused as to how i can go about saving everything on leaderstats of a player for when they return.
If you place all the info you want to save in a table or dictionary object then save that to a single datastore with the player.userid as the key, you will just need to parse the data when loading.
E.g (pseudocode)
Local data = {
Money = player.leaderstats.money.value,
Xp = player.leaderstats.xp.value
}
store:setasync(tostring(player.userid), data)
What @Wigglyaa is essentially correct, you use a table to save multiple pieces of data and put them as one item.
Creating Keys for Multile Pieces of Data is Bad Practice, You would be creating multiple Keys in a DataStore, and to get this Data, you would need to Send a Request, DataStores can only handle so many Requests before failing, and you sending multiple for one piece of Data?
I think you can see the problem.
With tables, you can just store multiple pieces of Data, as one item, and save it as such.
This part is just here to answer or fix a few things people said.
A Couple of things,
Make sure you spell the function Correctly, Becuase Lua will look for this function. And a typo, or uncapitalized could prevent it from finding the function, and cause an error.
The Second thing is tostring is not required to svae the data, when you give a number as the key, Lua should still convert it into a string.
You can save tables to DataStores, People tend to use JSONDecode and JSONEncode to compact Data.