Tool not going into player's backpack

So, I’ve been trying to make it where if the player has a pickaxe in their inventory and tried to equip a different pickaxe, it would instead switch the currently equipped pickaxe with the pickaxe the player is trying to equipped for example

But I keep running into a problem, the tool never goes into the player’s backpack despite the game saying the player has it equipped. I don’t know how to fix this problem and was hoping if anyone could give any advice.

The script is a server script that is activated upon the player equipping it.

Script:

RS.RemoteEvents.EquipItem.OnServerEvent:Connect(function(player, Item)
	local ClonedItem = game.ServerStorage.Tools:FindFirstChild(Item):Clone()
	print(ClonedItem.Name)
	for _, Tool in pairs(player.Backpack:GetChildren()) do
		if game.ServerStorage.Tools:FindFirstChild(Item):FindFirstChild("MiningSpeed") then --Will check if the item is a pickaxe
			if Tool:FindFirstChild("MiningSpeed") then 
			 	--Will check if any of the tools in the hotbar is a pickaxe
				--If so it will replace the currently equipped pickaxe with the one the player is trying to equip

				Tool:Destroy()
				ClonedItem.Parent = player.Backpack
			else  --If the item isn't a pickaxe it will just add the tool to the hotbar.
				ClonedItem.Parent = player.Backpack
			end
		end
	end
end)

Any help is very much appreciated!

1 Like

I’m not to certain but can you try to move the ClonedItem Variable before the line ClonedItem.Parent = player.Backpack

Only problem is it won’t give the tool to the player at all even if the player doesn’t have a pickaxe in their hotbar/Backpack

try

ClonedItem.Parent = player.Character

It never added it to the player’s character oddly enough

You could also use ‘Humanoid:EquipTool(ToolHere)’.

I’ve had something similar to that in the past,
you could check if the player has any item in class “Pickaxe” in his inventory, and if there is- unequip that from his backpack and put the new.( I usually use string values for that).
Currently on mobile so kind of difficult to provide a script.

1 Like

So I’ve realized that if I put ClonedItem.Parent = player.Backpack before for _, Tool in pairs(player.Backpack:GetChildren()) do it’ll add it to the player’s backpack but it won’t remove any other pickaxes in the player’s backpack. But if I put it in the
for _, Tool in pairs(player.Backpack:GetChildren()) do then it’ll just do nothing (won’t remove or add any items to/from the Hotbar.) with no errors in the output. I don’t know if this will help out but I thought it was something noteworthy.

Maybe have a StringValue called ‘Class’ in each tool, so that could be helpful to you in this case?

And you could just check through the loop if their class is ‘Pickaxe’ or different.

1 Like

The problem is checking the player’s backpack.
I’ve tried using a loop to check if there are any tools that are classified as a “Pickaxe” in the player’s backpack
But the loop prevents everything in the loop to function, I don’t know the cause of this however

managed to find out the problem myself, apparently, I had to clone the item outside of the for in pairs do function and destroy the item that was being replaced with the cloned item inside of the for do function.
Here’s the script if anyone wants to see it. Thank you everyone for your help!

	local Amount = #player.Backpack:GetChildren()
	local ClonedItem = game.ServerStorage.Tools:FindFirstChild(Item):Clone()
	
	for i, Tool in pairs(player.Backpack:GetChildren()) do
		print(Tool.Name)
		
		if ClonedItem:FindFirstChild("ToolType").Value == Tool:FindFirstChild("ToolType").Value then
			Tool:Destroy()
		end
	end
	ClonedItem.Parent = player.Backpack