01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "DataBubble" ) |
02 |
03 | game.Players.PlayerAdded:connect( function (player) |
04 |
05 | local Folder = Instance.new( "Folder" ,player) |
06 | Folder.Name = "Folder" |
07 |
08 | local BubblePoints = Instance.new( "IntValue" ,Folder) |
09 | BubblePoints.Name = "Points" |
10 |
11 | local BubbleLevel = Instance.new( "IntValue" ,Folder) |
12 | BubbleLevel.Name = "Level" |
13 |
14 | local BubbleCoins = Instance.new( "IntValue" ,Folder) |
15 | BubbleCoins.Name = "Coins" |
I'm trying to save three different values, but none of them are actually saving. Where did I go wrong? XD
Lua tables are indexed from 1 not 0 so this:-
1 | BubblePoints.Value = SavedValues [ 0 ] -- there is not index 0 |
2 | BubbleCoins.Value = SavedValues [ 1 ] |
3 | BubbleLevel.Value = SavedValues [ 2 ] |
Needs to be changed to:-
1 | BubblePoints.Value = SavedValues [ 1 ] |
2 | BubbleCoins.Value = SavedValues [ 2 ] |
3 | BubbleLevel.Value = SavedValues [ 3 ] |
You seems to be saving the table correctly and loading the data correctly, you can unpack an array to see all of the data e.g.
1 | print ( unpack ( [ array herer ] )) |
this will print all of the content of the array.
Hope this helps.
Including what kingcom5 stated about arrays in Lua, the problem is when the player leaves you're resetting the datastore to your 'ValuesToSave' variable, which is a set table and doesn't actually consist of any values from the Player.
Get the values from the player!
1 | game.Players.PlayerRemoving:connect( function (player) |
2 | local key = "player-" ..player.UserId |
3 | local ValuesToSave = { |
4 | player.Points.Value |
5 | player.Coins.Value, |
6 | player.Level.Value |
7 | } |
8 | DataStore:SetAsync(key, ValuesToSave) |
9 | end ) |