#tools data store help

1 messages · Page 1 of 1 (latest)

sick flint
#
local function LoadData(player)
    local data
    pcall(function()
        data = PlayerDataStore:GetAsync("Player_" .. player.UserId)
    end)

    if not data then return end

    if data.Attributes then
        for name, value in pairs(data.Attributes) do
            player:SetAttribute(name, value)
        end
    end

    for _, item in ipairs(player.Backpack:GetChildren()) do
        if item:IsA("Tool") then
            item:Destroy()
        end
    end

    if data.Tools then
        for _, toolName in ipairs(data.Tools) do
            local tool =
                AxesFolder:FindFirstChild(toolName)
                or LootsFolder:FindFirstChild(toolName)

            if tool then
                tool:Clone().Parent = player.Backpack
            end
        end
    end
end

local function SaveData(player)
    if player.Character then
        for _, tool in ipairs(player.Character:GetChildren()) do
            if tool:IsA("Tool") then
                tool.Parent = player.Backpack
            end
        end
    end

    task.wait()

    local saveData = {
        Attributes = {},
        Tools = {}
    }

    for _, attr in ipairs(ATTRIBUTES) do
        saveData.Attributes[attr] = player:GetAttribute(attr)
    end

    for _, tool in ipairs(player.Backpack:GetChildren()) do
        if tool:IsA("Tool") then
            table.insert(saveData.Tools, tool.Name)
        end
    end

    pcall(function()
        PlayerDataStore:SetAsync("Player_" .. player.UserId, saveData)
    end)
end

Players.PlayerAdded:Connect(function(player)
    LoadData(player)
end)

Players.PlayerRemoving:Connect(function(player)
    SaveData(player)
end)

game:BindToClose(function()
    for _, player in ipairs(Players:GetPlayers()) do
        SaveData(player)
    end
end)
``` why my tools data store are not working (the atributes are working but when i equip a tool and leave the tool is deleted)
vale otter
#

It's impossible that you still haven't solved this.

#

when you equip a tool she go to your player character model

#

that it

sick flint
#

Im dumb 💔

vale otter
#

Well you just need to force the player unquip the tool or just save directly from the character model, and when loaded, put it in the backpack.

#

if player.Character then
for _, tool in ipairs(player.Character:GetChildren()) do
if tool:IsA("Tool") then
tool.Parent = player.Backpack
end
end
end

vale otter
#

But that won't work here because I think the "ipairs" part should say "pairs".