Issues - problems trying to remove tool from player

I’m trying to get the existing tool to be removed when it’s “turn off” and it’s not working, what could be wrong? It tells me “print("Tool not found in backpack.")

localscript:

local chooseElectricGuitar = script.Parent.Frame:WaitForChild("On/Off")
local isOn = false
local debounce = false
local hasTool = false  -- Flag to track if the tool has been created

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local remoteEventAddGuitar = replicatedStorage:WaitForChild("remoteEventAddGuitar")
local remoteEventRemoveGuitar = replicatedStorage:WaitForChild("remoteEventRemoveGuitar")
local tool = replicatedStorage:WaitForChild("ElectricGuitar")

chooseElectricGuitar.MouseButton1Click:Connect(function()
	local player = players.LocalPlayer

	if not debounce then
		debounce = true

		if isOn then
			-- Turn off the guitar
			print("Turning off...")
			local guitarToRemove = player.Backpack:FindFirstChild("ElectricGuitar")
			if guitarToRemove then
				remoteEventRemoveGuitar:FireServer(guitarToRemove)
			end
			hasTool = false  -- Reset the flag

		else
			-- Turn on the guitar
			if not player.Backpack:FindFirstChild("ElectricGuitar") then  -- Check if tool already exists
				local clonedTool = tool:Clone()
				clonedTool.Parent = player.Backpack
				remoteEventAddGuitar:FireServer(player, clonedTool)
				hasTool = true  -- Set the flag to indicate the tool is created
			else
				print("Tool already exists in backpack.")
			end
		end

		chooseElectricGuitar.Text = isOn and "Off" or "On"
		isOn = not isOn
		wait(0.5)
		debounce = false
	end
end)

server-side script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEventRemoveGuitar = replicatedStorage:WaitForChild("remoteEventRemoveGuitar")

remoteEventRemoveGuitar.OnServerEvent:Connect(function(player, toolName)
	print("Server received request to remove tool:", toolName)

	-- Find the tool in the player's backpack
	local toolToRemove = player.Backpack:FindFirstChild(player, toolName)

	if toolToRemove then
		print("Tool found in backpack.")
		toolToRemove:Destroy()
		print("Tool removed successfully.")
	else
		print("Tool not found in backpack.")
	end
end)

If the player has the tool equipped, then it appears in the players character model, not the backpack. So you’re also going to have to check it there.

1 Like

Do you think you can give me a hand, so how can I correct it?

Sure.

While looking into this I also realised you had another issue, that is this line:
local toolToRemove = player.Backpack:FindFirstChild(player, toolName)
It threw an error because you passed two parameters, only one was required.

To fix your script, you only need to modify this one line by changing it into:
local toolToRemove = player.Backpack:FindFirstChild(toolName) or player.Character:FindFirstChild(toolName)

Hoped this help, lmk if you still run into issues.

1 Like

This printed the following:

in my code server script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEventRemoveGuitar = replicatedStorage:WaitForChild("remoteEventRemoveGuitar")

remoteEventRemoveGuitar.OnServerEvent:Connect(function(player, toolName)
	print("Server received request to remove tool:", toolName)

	-- Check for tool in backpack first
	print("toolName value:", toolName)
	local toolToRemove = player.Backpack:FindFirstChild(toolName) or player.Character:FindFirstChild(toolName)

	if toolToRemove then
		print("Tool found in backpack.")
		toolToRemove:Destroy()
		print("Tool removed successfully.")

		-- If not in backpack, check the character model
	elseif player.Character then
		local character = player.Character
		local humanoid = character:FindFirstChild("Humanoid")
		local equippedTool = humanoid and humanoid:FindFirstChildOfClass("Tool")

		if equippedTool and equippedTool.Name == toolName then
			print("Tool found equipped on character.")
			humanoid:UnequipTools()  -- Unequip the tool
			equippedTool:Destroy()
			print("Tool removed successfully.")
		else
			print("Tool not found in backpack or equipped.")
		end
	else
		print("Character model not found.")
	end
end)

thanks it worked! now it is fixed

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.