Save tools not saving equipped tool

Hello, So I have a script that saves a players tools, but the tool that is equipped when they leave doesn’t save, I need help on fixing this, here is the script

local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
 local key = "id-"..plr.userId
 pcall(function()
  local tools = ds:GetAsync(key)
  if tools then
   for i,v in pairs(tools) do
    local tool = game.ServerStorage.Tools:FindFirstChild(v)
    if tool then
     tool:Clone().Parent = plr:WaitForChild("Backpack")
     tool:Clone().Parent = plr:WaitForChild("StarterGear")
    end
   end
  end
 end)
end)
game.Players.PlayerRemoving:connect(function(plr)
 local key = "id-"..plr.userId
 pcall(function()
  local toolsToSave = {}
  for i,v in pairs(plr.Backpack:GetChildren()) do
   if v then
    table.insert(toolsToSave,v.Name)
   end
  end
  ds:SetAsync(key,toolsToSave)
 end)
end)
1 Like

Equipped tools are stored in the player’s character. You’ll have to loop through the character using :IsA(“Tool”) to find the equipped tool.

I’d recommend forcing all tools to unequip first before saving to allow other codes to conclude what they’re doing.

1 Like

I am a bit confused on how to do that.

2 Likes

Do this before looping through the player’s backpack. It’ll put all equipped tools into the backpack.

1 Like

I most likely did it wrong because it didnt work, but heres what I did

local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
	local key = "id-"..plr.userId
	pcall(function()
		local tools = ds:GetAsync(key)
		if tools then
			for i,v in pairs(tools) do
				local tool = game.ServerStorage.Colas:FindFirstChild(v)
				if tool then
					tool:Clone().Parent = plr:WaitForChild("Backpack")
					tool:Clone().Parent = plr:WaitForChild("StarterGear")
				end
			end
		end
	end)
end)
game.Players.PlayerRemoving:connect(function(plr)
	local key = "id-"..plr.userId
	pcall(function()
		local toolsToSave = {}
		game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
		for i,v in pairs(plr.Backpack:GetChildren()) do
			if v then
				table.insert(toolsToSave,v.Name)
			end
		end
		ds:SetAsync(key,toolsToSave)
	end)
end)

Can you show me how to do it?

1 Like

I don’t have a device that I can script with me right now, so I can’t do much. But from looking at the script, it’s possible that:

userId needs to be UserId

pairs needs to be ipairs

Can you please show me the output?

1 Like

It doesnt save now that I added the new Unequipt(), also nothing in output

1 Like

Try printing out the whole process so you know where it went wrong. Print out the tools saved, backpack, tools loaded, and anything else that can make you track the process.

I am sorta tired and confused, I am just going to stop developing for now, when are you able to get a device to script on, because I need a bit of help on this, I am still new to lua

1 Like