#data not loading when tool is in character

1 messages · Page 1 of 1 (latest)

stray lotus
#
local datastoreService = game:GetService("DataStoreService")
local PlayerTools = datastoreService:GetDataStore("PlayerTools")


game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(character)
        
        
        print("10 seconds pased")
        
        local suucces, inventory = pcall(function()
            return PlayerTools:GetAsync(plr.UserId)
        end)
        if suucces then
            for _, toolName in inventory do
            print(toolName)
            local tools = game.ReplicatedStorage.Tools
            local tool = tools:FindFirstChild(toolName)
            print(tool.Name, "tool found")
            tool:Clone().Parent = plr.Backpack
            end
        else
            print("Could not get data")
        end
    end)
end)


game.Players.PlayerRemoving:Connect(function(plr)
        local backpack = plr.Backpack
        local char = plr.Character
        local myTools = {}
        if backpack then
            for _, tool in backpack:GetChildren() do
                if tool:IsA("Tool") then
                    table.insert(myTools,tool.Name)
                    print(myTools[#myTools], 0)
                end
            end
        end
        
        if char then
            for _, tool in char:GetChildren() do
                if tool:IsA("Tool") then
                    table.insert(myTools, tool.Name )
                    print(myTools[#myTools],1)
                end
            end
        end
        print(myTools)

        local succes, err = pcall(function()
            return PlayerTools:SetAsync(plr.UserId, myTools)
        end)
        if succes then
            print(myTools[#myTools], 2)
        else
            print(err)
        end
    
    
end)```
data not saving when the tool is equipped(when is in the character), Ive tried plr.CharacterRemoving, didnt worked, any suggestions?
white rampartBOT
#

studio** You are now Level 8! **studio

warm juniper
#

Try ts

#

local datastoreService = game:GetService("DataStoreService")
local PlayerTools = datastoreService:GetDataStore("PlayerTools")
local playerInventories = {}

local function updateInventory(plr)
local myTools = {}
for _, tool in plr.Backpack:GetChildren() do
if tool:IsA("Tool") then
table.insert(myTools, tool.Name)
end
end
if plr.Character then
for _, tool in plr.Character:GetChildren() do
if tool:IsA("Tool") then
table.insert(myTools, tool.Name)
end
end
end
playerInventories[plr.UserId] = myTools
end

game.Players.PlayerAdded:Connect(function(plr)
local success, inventory = pcall(function()
return PlayerTools:GetAsync(plr.UserId)
end)
if success and inventory then
local toolsFolder = game.ReplicatedStorage:WaitForChild("Tools")
for _, toolName in ipairs(inventory) do
local tool = toolsFolder:FindFirstChild(toolName)
if tool then
tool:Clone().Parent = plr.Backpack
end
end
end
plr.Backpack.ChildAdded:Connect(function() updateInventory(plr) end)
plr.Backpack.ChildRemoved:Connect(function() updateInventory(plr) end)
plr.CharacterAdded:Connect(function(char)
char.ChildAdded:Connect(function() updateInventory(plr) end)
char.ChildRemoved:Connect(function() updateInventory(plr) end)
end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
updateInventory(plr)
local toolsToSave = playerInventories[plr.UserId] or {}
local success, err = pcall(function()
PlayerTools:SetAsync(plr.UserId, toolsToSave)
end)
if not success then
warn(err)
end
end)

stray lotus
warm juniper
stray lotus
stray lotus
#

that only when the tool is equipped, same problem again lmao, this will haunt me till I die