Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

Datastore not working correctly?

Asked by
Kitorari 262 Moderation Voter
2 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
kingdom5 5316 Trusted Snack Break Moderation Voter Community Moderator
2 years ago
Edited 2 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 262 — 2y
Ad
Log in to vote
1
Answered by
Goulstem 8005 Badge of Merit Moderation Voter Administrator Community Moderator
2 years ago
Edited 2 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 262 — 2y

Answer this question