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

Is it possible to add values to an existing DataStore?

Asked by 4 years ago

Hey all, I have an existing datastore containing player data that I want to add a new value too.

Here is the code I used. When I first coded this into my game, I created it with Points, Survivals, Level, and XP as the only values. Now that I have added the Colour value, it won't save/load to previously existing player data, presumably because I'm trying to update a value that does not exist in their datastore. (leaderstats is what appears on the leaderboard, and hiddenstats are stats that aren't public to others)

local Data = game:GetService('DataStoreService'):GetDataStore('Data0')

local prefix = 'user_'

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local hiddenstats = player:WaitForChild("hiddenstats")
    local points = leaderstats:WaitForChild("Points")
    local survivals = hiddenstats:WaitForChild("Survivals")
    local level = leaderstats:WaitForChild("Level")
    local xp = hiddenstats:WaitForChild("XP")
    local colour = hiddenstats:WaitForChild("Colour")

    local plrData = Data:GetAsync(prefix  .. tostring(player.UserId))

    if plrData then
          points.Value = plrData[1] or 0
          survivals.Value = plrData[2] or 0
          level.Value = plrData[3] or 1
          xp.Value = plrData[4] or 0
          colour.Value = plrData[5] or 7
    else

    Data:SetAsync(prefix  .. tostring(player.UserId), {
        points.Value,
        survivals.Value,
        level.Value,
        xp.Value,
        colour.Value        
     })
   end

end)

game.Players.PlayerRemoving:connect(function(player)
    Data:SetAsync(prefix  .. tostring(player.UserId), {
        player.leaderstats.Points.Value,
        player.hiddenstats.Survivals.Value,
        player.leaderstats.Level.Value,
        player.hiddenstats.XP.Value,
        player.hiddenstats.Colour.Value
    })
end)

I think I understand the problem, but I'm not entirely sure how I could work this into my code. Any help or suggestions would be greatly appreciated!

1 answer

Log in to vote
0
Answered by 4 years ago

I don't think there is a way to edit an existing DataStore unless you use the DataStore Editor plugin as of here.

0
I downloaded the plugin and watched the tutorial, but the game won't even recognize my place ID, it just gives me an error 403 when I try to load in anything... Elliott_RBLX 15 — 4y
0
The plugin would help a lot with changing peoples data anyway, if I could get it to work, but I'm more looking for something I can work into my data script to 'update' someones data, so I don't have to do it manually every single time i add a new stat to the game Elliott_RBLX 15 — 4y
0
I don't think there is a proper way to update a datastore, cause it creates a new datastore when adding new stats. Fl3eandFlo3e 18 — 4y
0
I read some other forum posts of people doing it, I just wasn't sure as to how I could update my own script Elliott_RBLX 15 — 4y
Ad

Answer this question