#Tools not saving when the player holds them

1 messages · Page 1 of 1 (latest)

neon portal
#

i made an script where when a player leaves tools inside of the players inventory get saved. i realized that tools held by players dont get saved so i added an save for held items but that doesnt work too, anyone can help?

#
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ToolStore = DataStoreService:GetDataStore("PlayerTools_V3")
local ToolFolder = ReplicatedStorage:WaitForChild("Tools")

local PlayerToolMemory = {}

local function registerTool(player, tool)
    if not tool:IsA("Tool") then return end

    PlayerToolMemory[player] = PlayerToolMemory[player] or {}
    PlayerToolMemory[player][tool.Name] = true
end


local function watchContainer(player, container)
    container.ChildAdded:Connect(function(child)
        if child:IsA("Tool") then
            registerTool(player, child)
        end
    end)
end

local function onPlayerAdded(player)
    PlayerToolMemory[player] = {}

    player:WaitForChild("Backpack")
    watchContainer(player, player.Backpack)

    player.CharacterAdded:Connect(function(character)
        watchContainer(player, character)
    end)

    local success, saved = pcall(function()
        return ToolStore:GetAsync(player.UserId)
    end)

    if success and type(saved) == "table" then
        for toolName in pairs(saved) do
            local template = ToolFolder:FindFirstChild(toolName)
            if template then
                template:Clone().Parent = player.Backpack
            end
        end
    end
end

local function onPlayerRemoving(player)
    local owned = {}

    local backpack = player:FindFirstChild("Backpack")
    if backpack then
        for _, tool in ipairs(backpack:GetChildren()) do
            if tool:IsA("Tool") then
                owned[tool.Name] = true
            end
        end
    end

    local character = player.Character
    if character then
        for _, tool in ipairs(character:GetChildren()) do
            if tool:IsA("Tool") then
                owned[tool.Name] = true
            end
        end
    end

    local success, err = pcall(function()
        ToolStore:UpdateAsync(player.UserId, function()
            return owned
        end)
    end)

    if not success then
        warn("Failed to save tools:", err)
    end
end




game:BindToClose(function()
    for _, player in ipairs(Players:GetPlayers()) do
        onPlayerRemoving(player)
    end
end)



Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
gritty trail
#

Perhaps char isn’t loaded yet

neon portal
#

bullshit

steel condor
oblique willow
oblique willow
#

@neon portal

#

I am pretty sure the reason why your script isn't working is due 2 the onPlayerRemoving function you should do SetAsync not UpdateAsync

neon portal
#

oh?

neon portal
steel condor
#

gotta love roblox studio 🙏