it saves the tools in backpack but when I hold it or equip it and leave it doesn’t save only if I don’t equip it I dpon’t know how to fix it litarilly
local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("Saved")
local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SaveData")
-- Tracks currently equipped tool per player
local EquippedTools = {}
game.Players.PlayerAdded:Connect(function(Player)
print("[JOIN] Player added:", Player.Name)
local ToolData
local success, err = pcall(function()
ToolData = SaveData:GetAsync(Player.UserId)
end)
if success then
print("[LOAD] Tool data found for", Player.Name, ":", ToolData)
else
warn("[LOAD ERROR] Failed to get data for", Player.Name, ":", err)
end
local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")
if ToolData then
for _, toolName in ipairs(ToolData) do
local Tool = ToolFolder:FindFirstChild(toolName)
if Tool and not Backpack:FindFirstChild(toolName) and not StarterGear:FindFirstChild(toolName) then
print("[LOAD] Giving tool to", Player.Name, ":", toolName)
local ToolClone1 = Tool:Clone()
local ToolClone2 = Tool:Clone()
ToolClone1.Parent = Backpack
ToolClone2.Parent = StarterGear
else
warn("[LOAD] Tool missing or already exists:", toolName)
end
end
end
Player.CharacterAdded:Connect(function(Character)
print("[CHARACTER] Spawned for", Player.Name)
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.Equipped:Connect(function(Tool)
if Tool:IsA("Tool") then
EquippedTools[Player.UserId] = Tool.Name
print("[EQUIPPED]", Player.Name, "equipped", Tool.Name)
end
end)
Humanoid.Unequipped:Connect(function(Tool)
if Tool:IsA("Tool") then
if EquippedTools[Player.UserId] == Tool.Name then
print("[UNEQUIPPED]", Player.Name, "unequipped", Tool.Name)
EquippedTools[Player.UserId] = nil
end
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
print("[LEAVE] Player is leaving:", Player.Name)
local ToolTable = {}
for _, Tool in ipairs(Player.Backpack:GetChildren()) do
table.insert(ToolTable, Tool.Name)
print("[SAVE] Tool in Backpack:", Tool.Name)
end
local equippedTool = EquippedTools[Player.UserId]
if equippedTool then
print("[SAVE] Equipped tool also being saved:", equippedTool)
table.insert(ToolTable, equippedTool)
end
local success, err = pcall(function()
SaveData:SetAsync(Player.UserId, ToolTable)
end)
if success then
print("[SAVE SUCCESS] Tools saved for", Player.Name)
else
warn("[SAVE ERROR] Could not save tools for", Player.Name, ":", err)
end
EquippedTools[Player.UserId] = nil
end)