Need help with DataStore

I am trying to save the children of a folder however its not working there is no errors and I can’t seem to find the issue I looked on multiple dev forums on this and nothing helped me can someone please try and help.


game.Players.PlayerAdded:Connect(function(player)
    local plush = Instance.new("Folder")
    plush.Name = "Plushies"
    plush.Parent = player
    
    local key = "Found" -- Set your Key
    local datastore =  game:GetService("DataStoreService"):GetDataStore("Plushies") -- DataStore Name
    local datatable = {}
    local tablenum = 0
    local folder = player.Plushies  -- Folder
        for _,value in pairs(folder:GetChildren()) do --Only Values can be in the Folder
        tablenum = tablenum + 1
        table.insert(datatable,tablenum,{["Value"] = value.Value;["Name"] = value.Name})
    end
    local success, err = pcall(function()
        datastore:UpdateAsync(key, function(old) -- Updates Data
            local new = old or nil
            new = datatable
            return new
        end)
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    --Create Values
    local datastore =  game:GetService("DataStoreService"):GetDataStore("Plushies") -- DataStore Name
    local datatable = datastore:GetAsync(player.userId)
    if datatable then
        local folder = player.Plushies-- Folder
        local tablenum = 0
        for _,value in pairs(folder:GetChildren()) do --Only Values can be in the Folder
            local data = nil
            for i = 1, #datatable do
                local check = datatable[i]
                local name = check["Name"]
                if name and value.Name == name then
                    data = check
                end
            end
            if data ~= nil then
                if value.Name == data.Name then
                    value.Value = data.Value
                end
            end
        end
    end
end)

If anyone could help me, that would be grand.

1 Like

I haven’t fully read your code, but I saw something missing which is game:BindToClose(). Sometimes, Roblox Studio doesn’t properly close the test and stops certain scripts. So in your case, it doesn’t let it save. Fortunately, there’s a solution: How to properly save player data in data stores upon server close

1 Like