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

Saving newly created Values in a DataStore? [Closed]

Asked by 7 years ago
Edited 7 years ago

Hello I'm working on a DataStore script for a game I plan to release real soon and to make updating it in the future a lot easier I need to figure out how to save any newly created values in the datastore. For example whenever a player buys a specific item, instead of having a value already written in the DataStore script I want to create the specific instance of the value and it's value and have those saved instead. Is this doable and if so how would I go about doing this?

Here's some of my current DataStore script:

local key = "user-"..plyr.userId

local storeditems = DataStore:GetAsync(key)

if storeditems then
    money.Value = storeditems[1]
    shirt.Value = storeditems[2]
    pants.Value = storeditems[3]

    for i,v in pairs(storage:GetChildren()) do
        v.Value = storeditems[i+3]
    end
else
    repeat 
        wait(5*60)
        local items =
 {plyr.Data.Money.Value,plyr.Data.CurrentShirt.Value,plyr.Data.CurrentPants}

        for i,v in pairs(plyr.Data.Storage:GetChildren()) do
            table.insert(items,i+3,plyr.Data.Storage[v.Name].Value) 
        end --Save any newly bought items?

        print("Saving Data...")
        DataStore:SetAsync(key, items)
        DataStore:SetAsync(plyr.userId,os.time())
        wait(2)
        print("Data Saved!")
    until plyr == nil
end

Thanks in advance for any help!

0
I fixed your tabbing. Please do this yourself in the future. OldPalHappy 1477 — 7y
0
That doesn't really help.... My tabbing was fine, makes it easier to read for me. Nvm I figured out a solution. OneTruePain 191 — 7y

1 answer

Log in to vote
-3
Answered by 7 years ago

I don't really see what your trying to ask, but you can try this:

function onPlayerEntered(player)
wait()
player:WaitForDataReady()
repeat wait() until player:FindFirstChild("leaderstats")
if player.DataReady then
if player:findFirstChild("leaderstats") then
local score = player.leaderstats:GetChildren()
for i = 1,#score do
local ScoreLoaded = player:LoadNumber(score[i].Name)
wait()
if ScoreLoaded ~= 0 then
score[i].Value = ScoreLoaded
end
end
end
end
end

function onPlayerLeaving(player)
if player:findFirstChild("leaderstats") then
local score = player.leaderstats:GetChildren()
for i = 1,#score do
player:SaveNumber(score[i].Name,score[i].Value)
end
end
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerLeaving)

Sorry if it dont work, but dont down vote me please, but upvote if it worked!

1
I downvoted because you didn't explain what you changed, or why you changed it. If you don't explain what you did and why, people can't learn, and this becomes a bad answer. OldPalHappy 1477 — 7y
1
The code is also awful. ScriptGuider 5640 — 7y
0
Data persistence is deprecated. cabbler 1942 — 7y
Ad

Answer this question