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
local DataStore = game:GetService("DataStoreService"):GetDataStore("DataBubble")

game.Players.PlayerAdded:connect(function(player)

    local Folder = Instance.new("Folder",player)
    Folder.Name = "Folder"

    local BubblePoints = Instance.new("IntValue",Folder)
    BubblePoints.Name = "Points"

    local BubbleLevel = Instance.new("IntValue",Folder)
    BubbleLevel.Name = "Level"

    local BubbleCoins = Instance.new("IntValue",Folder)
    BubbleCoins.Name = "Coins"

    local key = "player-"..player.UserId

    local SavedValues = DataStore:GetAsync(key)

    if SavedValues then
        BubblePoints.Value = SavedValues[0]
        BubbleCoins.Value = SavedValues[1]
        BubbleLevel.Value = SavedValues[2]
    else
        local valuesToSave = {0, 1, 2}
        DataStore:SetAsync(key,valuesToSave)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    local key = "player-"..player.UserId

    local ValuesToSave = {0, 1, 2}

    DataStore:SetAsync(key, ValuesToSave)
end)

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:-

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

Needs to be changed to:-

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

print(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!

game.Players.PlayerRemoving:connect(function(player)
    local key = "player-"..player.UserId
    local ValuesToSave = {
        player.Points.Value
        player.Coins.Value, 
        player.Level.Value
    }
    DataStore:SetAsync(key, ValuesToSave)
end)
0
Got it >:D Kitorari 266 — 8y

Answer this question