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

DataStore wipes when I update?

Asked by
wackem 50
9 years ago

So whenever I update my game with this DataStore script it sets every player's stats back to the default they first had.

DataStore = game:GetService("DataStoreService"):GetDataStore("Values")


game.Players.PlayerAdded:connect(function(plr)
    local rank = Instance.new("StringValue", plr)
    rank.Value = "Asteroid Recruit"
    rank.Name = "Rank"

    local mined = Instance.new("IntValue", plr)
    mined.Value = 0
    mined.Name = "Mined"

    local save = "plr-"..plr.userId

    local savedVals = DataStore:GetAsync(save)

    if savedVals then
        rank.Value = savedVals[1]
        mined.Value = savedVals[2]
    else
        local val = {rank.Value, mined.Value}
        DataStore:SetAsync(save, val[1])
        DataStore:SetAsync(save, val[2])
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local val = {plr.Rank.Value, plr.Mined.Value}
    local save = "plr-"..plr.userId
    DataStore:SetAsync(save, val[1])
    DataStore:SetAsync(save, val[2])
end)

And I don't know why

-- Any help is appreciated

1 answer

Log in to vote
0
Answered by
funyun 958 Moderation Voter
9 years ago

Are you saving tables? If you are, you should be using JSONEncode and JSONDecode. These are functions of HttpService, and they allow you to convert tables to strings, and JSON strings to tables.

First, make sure that HttpService is enabled. Then...

http = game:GetService("HttpService")

t = {
    Level = 1,
    Gold = 100
}

jt = http:JSONEncode(t)
print(jt)

And basically, you can use this to save JSON strings rather than tables.

0
So what I should do is insert the values into a table and JSONEncode it? I don't understand JSONEncoding, I've never played with HTTPService wackem 50 — 9y
Ad

Answer this question