#datastore not saving correctly

2 messages · Page 1 of 1 (latest)

meager island
#

the title says it all. watch the video for more context.

client side:

for _, box in pairs(list:GetDescendants()) do
    if not box:IsA("TextBox") then continue end

    box.FocusLost:Connect(function()
        if debounce then
            return
        end

        debounce = true
        box.Interactable = false

        if box.Text:match("^%s*$") then
            box.Text = box.PlaceholderText
        else        
            local save_name = box.Parent.Name
            local new = box.Text
            
            local updates = {
                path = {save_name, "name"}, value = new
            }
            update:FireServer(updates)
        end

        task.wait(3)
        debounce = false
        box.Interactable = true
    end)
end
#

server side:

local function load(plr)
    local key = "saves_"..plr.UserId
    local success, data = pcall(function()
        return ds:GetAsync(key)
    end)

    if success then
        if data then
            merge(data, def)
            return data
        else
            return copy(def)
        end
    else
        return load(plr)
    end
end

local function save(plr, data)
    local key = "saves_"..plr.UserId
    local success, err = pcall(function()
        ds:SetAsync(key, data)
    end)

    if not success then
        warn("Failed to save save(s) for "..plr.Name..": "..err)
    end
end

local function update(plr, updates)
    local data = load(plr)

    for _, update in ipairs(updates) do
        local path = update.path
        local val = update.value

        local t = data
        for k = 1, #path - 1 do
            if t[path[k]] == nil then
                return
            end
            t = t[path[k]]
        end

        t[path[#path]] = val
    end

    save(plr, data)
    local new = load(plr)
    events.update:FireClient(plr, new)
end

events.update.OnServerEvent:Connect(update)

it may be the path logic i tried to do; the picture shows how deep into the datastore table the save name is. save_name will always match the table ("save1", "save2", etc)