Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Datastore not working correctly?

Asked by
Kitorari 266 Moderation Voter
8 years ago
01local DataStore = game:GetService("DataStoreService"):GetDataStore("DataBubble")
02 
03game.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"
View all 37 lines...

I'm trying to save three different values, but none of them are actually saving. Where did I go wrong? XD

2 answers

Log in to vote
7
Answered by 8 years ago
Edited 8 years ago

Lua tables are indexed from 1 not 0 so this:-

1BubblePoints.Value = SavedValues[0] -- there is not index 0
2BubbleCoins.Value = SavedValues[1]
3BubbleLevel.Value = SavedValues[2]

Needs to be changed to:-

1BubblePoints.Value = SavedValues[1]
2BubbleCoins.Value = SavedValues[2]
3BubbleLevel.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.

1print(unpack([array herer]))

this will print all of the content of the array.

Hope this helps.

0
Ohh okay. Mybad, Thank you! Kitorari 266 — 8y
Ad
Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

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!

1game.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)
9end)
0
Got it >:D Kitorari 266 — 8y

Answer this question