Answered by
5 years ago Edited 5 years ago
Hello,
To solve your saving multiple items to your datastore problem you need to save data, to the datastore, as an array.
Something like this:
1 | game.Players.PlayerRemoving:connect( function (player) |
2 | local items = { player.leaderstats.Points.Value, player.leaderstats.Gold.Value } |
3 | local key = "user-" .. player.userId |
5 | datastore:SetAsync(key, items) |
For your information, the above code saves data to the datastore when the player leaves the game. Datastores work with saving a data to a key. In this case, your key is the players userId. SetAsync is just syntax for saving or updating data to that key.
As you are adding more data, you need to change this part too:
1 | local storeditems = datastore:GetAsync(key) |
3 | Points.Value = storeditems [ 1 ] |
4 | Gold.Value = storeditems [ 2 ] |
What the above code is doing is getting all of the data for the key. For example, if the player with the userId 345237434 had 100 points and 12 gold. Then the storeditems variable would be equal to {100,12}. So, after checking if the player actually has any points or gold (if they have played the game before), we load the players amount of points and gold into the leaderstats values.
I hope this helped.