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

converting data persistence to Data stores?

Asked by 8 years ago

question says it all, i have a big game with many players that dont want to lose their data persistence, and for them to lose it would mean losing the players, and losing front page. Ive only found out recently i need data stores to transfer data across multiple places in a universe (i think). so i was wondering if maybe there was a way for when the player enters, load their data persistance, and then save it to data store (without loading it up first)

Data store code i have is here:

function onPlayerEntered(newPlayer)
        --Clone Data--
        local vs = script:WaitForChild("leaderstats"):clone()
        vs.Parent = newPlayer
        --Setup Intial Data--
        local Stats = vs:GetChildren()
        local S1 = {}
        for i=1,#Stats do
            local n = Stats[i].Name
            S1[n] = Stats[i].Value
        end
        --Get Data--
        if newPlayer.Name ~= "Player" then

            newPlayer:WaitForDataReady() -- This is the data persistence here;
            local score1 = newPlayer:LoadNumber("Lvl")

            if score1.Value <= 1 then -- maybe if the level value <=  1 then make data store?

            end  -- else, load data persistence, then delete it? (so it doesnt have to load it again?)

            local DataStore = game:GetService("DataStoreService"):GetDataStore(newPlayer.userId)

            if DataStore:GetAsync("Stats") == nil then
                DataStore:SetAsync("Stats", S1)
            end
            S1 = DataStore:GetAsync("Stats")

        end
        --Safely Load Data--
        local vv = vs:GetChildren()
        for i=1,#vv do
            local n = vv[i].Name
            --print(S1[n])
            if S1[n] ~= nil then
                vv[i].Value = S1[n]
            end
        end
end

game.Players.PlayerAdded:connect(onPlayerEntered)

for _,player in pairs(game.Players:GetChildren()) do
    onPlayerEntered(player)
end

game.Players.PlayerRemoving:connect(function(p)
    --Save Data--
    local DataStore = game:GetService("DataStoreService"):GetDataStore(p.userId)
    local stats = p.Data:GetChildren()
    local s1 = {}
    for i=1,#stats do
        local n = stats[i].Name
        s1[n] = stats[i].Value
    end
    DataStore:SetAsync("Stats",s1)
    --print("Saved "..p.Name)
end)

thanks for any help ~ Bubs

1 answer

Log in to vote
1
Answered by
Edenojack 171
8 years ago

Thats actually what I was thinking. First, check if they have any Data Store saves, if there is nothing there, load up the Data Persistance, and transfer it to a Data Store. Then save that. From that point on, they are seen as having that data and it will no longer search for the old data (Which you don't need to call on.)

Because you may have players return to your game in a few months, it'd be useful to keep this as legacy.

Try it out in a seperate place before you go live though.

0
thanks, i was wondering if you could check the code i added? see what ya think Bubbles5610 217 — 8y
0
I'm still incredibly new to Data storage myself, so unfortunately I won't be much help Edenojack 171 — 8y
Ad

Answer this question