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

Issue with datastore saving whenever the player leaves, can someone help diagnose it?

Asked by 5 years ago
Edited 5 years ago

Okay- I am very much new to scripting, so there is a chance that the issue is very obvious. The code below -works-, as in, almost all of it but one single line.

local serverScriptService = game:GetService("ServerScriptService")
local dataStoreService = game:GetService("DataStoreService")
local httpService = game:GetService("HttpService")

local dataValues = script.dataValues

local serverModules = serverScriptService.serverModules
local dataStore = dataStoreService:GetDataStore("player_Data2")


--Load Data
game.Players.PlayerAdded:Connect(function(plr)
    local success, err = pcall(function()
    local key = "ID-".. tostring(plr.UserId)
    local getSave = dataStore:GetAsync(key)
    local dataFolder = Instance.new("Folder", plr)
    dataFolder.Name = "dataFolder"

    --Creates a copy of all datavalues and pastes them into DataFolder. It also makes sure each player has updated datavalues if I add a new one.
    for i, v in pairs(dataValues:GetChildren()) do
        local newData = Instance.new(v.ClassName)
        newData.Name = v.Name
        newData.Value = v.Value
        newData.Parent = dataFolder
    end
    --Check if the player already has data
    if getSave then
        --Has data, so we simply load the saved values into the values in the dataFolder
        for i, v in pairs(dataValues:GetChildren()) do
            local decodedData = getSave
            dataFolder[v.Name].Value = decodedData[i]
            print(i, v.Name, v.Value)
        end
    else
        --Does not have data, so we create an array of data for them.
        local saveData = {}
        for i, v in pairs(dataFolder:GetChildren()) do
            table.insert(saveData, v.Value)
            print(i, v.Name, v.Value)
        end
        local encodedData = httpService:JSONEncode(saveData)
        dataStore:SetAsync(key, encodedData)
    end
    end)
    if err then
        print("Error Loading Data.")
    end
end)

--Save Data
game.Players.PlayerRemoving:Connect(function(plr)
    local success, err = pcall(function()
        local dataFolder = plr:WaitForChild("dataFolder")
        local dataList = dataFolder:GetChildren()
        local key = "ID-".. tostring(plr.UserId)
        local saveData = {}
        for i, v in pairs(dataList) do
            table.insert(saveData, v.Value)
            print(i, v.Name, v.Value)
        end
        local encodedData = httpService:JSONEncode(saveData)
        dataStore:SetAsync(key, encodedData)
    end)
    if err then
        print("Error Saving Data.")
    end
end)

The issue comes in game.Players.PlayerRemoving... Specifically this line, " dataStore:SetAsync(key, encodedData)". Everything up to that point works, just for some reason it doesn't save the data, albeit, when the player joins, and has no data, the same code does work. So I am confused about this.

0
Quick question, you say "the issue comes in ... specifically this line ..." but what exactly is the error code you are receiving? SerpentineKing 3885 — 5y
0
FYI you don’t need to encode your data into JSON before saving because Roblox does that automatically for you. Rheines 661 — 5y
0
SerpentineKing, I wasn't receiving any error code, just the pcall was erroring out. But I fixed it below, figured out how to do so last night. But, I am not 100% sure the fix is the best thing. rockshe1000 20 — 5y
0
Thanks for the info Rheines! Guess I must've got faulty information 'cus I saw people claiming you need to do so with an array. I'll change that up then. rockshe1000 20 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Never mind guys! I actually found out how to fix it myself. The new code is documented below, albeit with some other changes, (other errors that I found in the here and there that I didn't notice yet) Basically, removing the old data first before then setting the new data is what I needed to do.

local serverScriptService = game:GetService("ServerScriptService")
local dataStoreService = game:GetService("DataStoreService")
local httpService = game:GetService("HttpService")

local dataValues = script.dataValues

local serverModules = serverScriptService.serverModules
local dataStore = dataStoreService:GetDataStore("player_Data2")


--Load Data
game:GetService("Players").PlayerAdded:Connect(function(plr)
    local success, err = pcall(function()
    local key = "ID-".. tostring(plr.UserId)
    local getSave = dataStore:GetAsync(key)
    local dataFolder = Instance.new("Folder", plr)
    dataFolder.Name = "dataFolder"

    --Creates a copy of all datavalues and pastes them into DataFolder. It also makes sure each player has updated datavalues if I add a new one.
    for i, v in pairs(dataValues:GetChildren()) do
        local newData = Instance.new(v.ClassName)
        newData.Name = v.Name
        newData.Value = v.Value
        newData.Parent = dataFolder
    end
    --Check if the player already has data
    if getSave then
        --Has data, so we simply load the saved values into the values in the dataFolder
        local dataList = dataFolder:GetChildren()
        for i, v in pairs(dataList) do
            local decodedData = httpService:JSONDecode(getSave)
            v.Value = decodedData[i]
            print(decodedData[i])
        end
        print(dataStore:GetAsync(key))
    else
        --Does not have data, so we create an array of data for them.
        local saveData = {}
        for i, v in pairs(dataFolder:GetChildren()) do
            table.insert(saveData, v.Value)
        end
        local encodedData = httpService:JSONEncode(saveData)
        dataStore:SetAsync(key, encodedData)
    end
    end)
    if err then
        print("Error Loading Data.")
    end
end)

--Save Data
game:GetService("Players").PlayerRemoving:Connect(function(plr)
    local success, err = pcall(function()
        local dataFolder = plr:WaitForChild("dataFolder")
        local dataList = dataFolder:GetChildren()
        local key = "ID-".. tostring(plr.UserId)
        dataStore:RemoveAsync(key)
        local saveData = {}
        for i, v in pairs(dataList) do
            table.insert(saveData, v.Value)
            print(i, v.Name, v.Value)
        end
        local encodedData = httpService:JSONEncode(saveData)
        dataStore:SetAsync(key, encodedData)
    end)
    if err then
        print("Error Saving Data.")
    end
end)
Ad

Answer this question