When equipped and I leave it removes

so here it saves my tools but if I have more of the same tool like for example 2 swords and equip one and leave and rejoi there is only 1 sword

local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("Saved")
local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SaveData")

-- Tracks equipped tools (optional)
local EquippedTools = {}

game.Players.PlayerAdded:Connect(function(Player)
	print("[JOIN]", Player.Name)

	-- Load saved tools from DataStore
	local ToolData
	local success, err = pcall(function()
		ToolData = SaveData:GetAsync(Player.UserId)
	end)

	if not success then
		warn("[LOAD ERROR] Failed to load for", Player.Name, ":", err)
	else
		print("[LOAD SUCCESS]", ToolData)
	end

	local Backpack = Player:WaitForChild("Backpack")
	local StarterGear = Player:WaitForChild("StarterGear")

	if ToolData and type(ToolData) == "table" then
		for _, toolName in ipairs(ToolData) do
			local Tool = ToolFolder and ToolFolder:FindFirstChild(toolName)
			if Tool then
				-- Clone to Backpack and StarterGear (your original behavior)
				local ToolClone1 = Tool:Clone()
				local ToolClone2 = Tool:Clone()
				ToolClone1.Parent = Backpack
				ToolClone2.Parent = StarterGear
				print("[LOAD] Gave", toolName, "to", Player.Name)
			else
				warn("[LOAD WARNING] Tool not found in ToolFolder:", toolName)
			end
		end
	end

	-- Track equipped tools (optional)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")

		Character.ChildAdded:Connect(function(child)
			if child:IsA("Tool") then
				EquippedTools[Player.UserId] = child.Name
				print("[EQUIPPED]", Player.Name, "equipped", child.Name)
			end
		end)

		Character.ChildRemoved:Connect(function(child)
			if child:IsA("Tool") and EquippedTools[Player.UserId] == child.Name then
				EquippedTools[Player.UserId] = nil
				print("[UNEQUIPPED]", Player.Name, "unequipped", child.Name)
			end
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	print("[LEAVE]", Player.Name)

	local ToolTable = {}

	-- Save all tools from Backpack (including duplicates)
	for _, tool in ipairs(Player.Backpack:GetChildren()) do
		if tool:IsA("Tool") then
			table.insert(ToolTable, tool.Name)
			print("[SAVE] Backpack tool:", tool.Name)
		end
	end

	-- Save all equipped tools from Character (including duplicates)
	if Player.Character then
		for _, tool in ipairs(Player.Character:GetChildren()) do
			if tool:IsA("Tool") then
				table.insert(ToolTable, tool.Name)
				print("[SAVE] Equipped tool:", tool.Name)
			end
		end
	end

	print("[SAVE] Tools to save:", table.concat(ToolTable, ", "))

	-- Save to DataStore
	local success, err = pcall(function()
		SaveData:SetAsync(Player.UserId, ToolTable)
	end)

	if success then
		print("[SAVE SUCCESS]", Player.Name)
	else
		warn("[SAVE ERROR]", Player.Name, ":", err)
	end

	EquippedTools[Player.UserId] = nil
end)
2 Likes

You could have a gap in array and ipairs is terminating because of it.
Using ipairs and pairs is no longer relevant in luau

just get the tool being equipped by the char using Character:FindFirstChildOfClass("Tool"), check if it exists, and puts it in the data.

Oh damn you told him that prior to me :joy::+1:
Yeah that the most likehood
He has to check character and backpack i completely forgor that its also the case
Non the less credits to you

1 Like

It’s fine lol, I just faced a similar problem days before and knew what to say

2 Likes

Try something like, Humanoid:UnequipTools()

1 Like

Did you try this… I’m not kidding this is the easy way to do that. I have a few save scripts made for what you’re doing here. One of them catch all calls… if there it removes it (to the backpack), if not doesn’t error. You may also what to look into JSONEncode and JSONDecode for your datastore.

This may be the easiest way, but it is not what the OP wants. When you use humanoid:UnequipTools(), you are setting all tools’ parent to the character. But this may look weird because you will unequip the tool abruptly. The best method for this is simply finding the tool inside the character and saving it (and it may be the most efficient one, since you are not using any methods).

It looks like they put it away as normal … I did say I have a few scripts doing the exacts same thing he is doing here. Do what you will but, consider actually checking what people take the time to try and help you with.

1 Like