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

My Data Store isn't loading values from my table in order?

Asked by 7 years ago

I'm saving a table in a Data Store but it doesn't load properly. I'm told it's not efficient to save values with a PlayerRemoving event, but I don't think it would jumble the values around.

If I change the value of strength in-game and rejoin, I'd get that changed value in dexterity!

    local key = "player-"..player.userId
    local savedStats = statsData:GetAsync(key)

    if savedStats then
        --Format: {lv,xp,str,dex,foc,vit}
        level.Value = savedStats[0]
        xp.Value = savedStats[1]
        strength.Value = savedStats[2]
        dexterity.Value = savedStats[3]
        focus.Value = savedStats[4]
        vitality.Value = savedStats[5]
    else
        local statsToSave = {level.Value, xp.Value, strength.Value, dexterity.Value, focus.Value, vitality.Value}
        statsData:SetAsync(key, statsToSave)
    end

end)

function saveStats(player)
    local key = "player-"..player.userId
    local statsToSave = {level.Value, xp.Value, strength.Value, dexterity.Value, focus.Value, vitality.Value}
    statsData:SetAsync(key, statsToSave)
end

game.Players.PlayerRemoving:connect(saveStats)

1 answer

Log in to vote
1
Answered by 7 years ago

You have made the mistake of using base 0 for the table, Lua table index

For lua the tables start from the index of 1 not 0.

I have not tested this but if this does not resolve the problem then comment and I will look at this again. Can you include the code on where you are making the instances level, xp ect.

Ad

Answer this question