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

My ToolData Isn't being saved?

Asked by 4 years ago

Basically, its not saving my data but I'm not sure why?

local ds = game:GetService("DataStoreService"):GetDataStore("Tools")

-- loading
game.Players.PlayerAdded:Connect(function(plr)
    local key = "id-"..plr.userId
    local success,tools = pcall(function()
        return ds:GetAsync(key)
    end)
    if  not success then
        error("not successful retrieving tooldata")

    else
        if not tools then return end
        for _, tool in pairs(tools) do
            local backpack = plr:WaitForChild("Backpack")
            local StarterGear = plr:WaitForChild("StarterGear")
            print(tool)
            if backpack and StarterGear then
                local toolInstance = game:GetService("ServerStorage").Tools:FindFirstChild(tool)
                if toolInstance then
                    print(toolInstance)
                    toolInstance:Clone().Parent = backpack
                    toolInstance:Clone().Parent = StarterGear
                end
            end
        end
    end

end)



-- saving
game.Players.PlayerRemoving:connect(function(plr)
 local key = "id-"..plr.userId
 local char = plr.Character or plr.CharacterAdded:Wait()

 for _,tool in pairs(char:GetDescendants()) do
     if tool:IsA("Tool") then
         tool.Parent = plr.Backpack
     end
 end

 local success, err = pcall(function()
        local toolsToSave = {}
        for i,v in pairs(plr.Backpack:GetChildren()) do
            if v then
                table.insert(toolsToSave,v.Name)
            end
         end
        print(unpack(toolsToSave))
        return ds:SetAsync(key,toolsToSave)
    end)
    if err then
        print(err)
    end
end)

Answer this question