The tool is unable to delete from the backpack

In short, I want the player to give a lantern when he presses F, but I want the item to be deleted when he presses F again.
Lantern is given but I’m having trouble while deleting it.

My server script:

local saladEvent = game.ReplicatedStorage.ToolServer2
local saladEvent2 = game.ReplicatedStorage.ToolServerEquip
local dleteitem = game.ReplicatedStorage.deletesitem

	saladEvent.OnServerEvent:Connect(function(plr, tool)
	local toolClone = tool:Clone()
	toolClone.Parent = plr.Backpack
end)

dleteitem.OnServerEvent:Connect(function(plr, tool)
	if plr.Backpack:FindFirstChild(tool) then
		plr.Backpack:FindFirstChild(tool):Destroy()
	end
end)

My Local script:

local weapon = game.ReplicatedStorage.Items.Lantern
local plr = game.Players.LocalPlayer

local saladEvent = game.ReplicatedStorage.ToolServer2
local saladEvent2 = game.ReplicatedStorage.ToolServerEquip
local dleteitem = game.ReplicatedStorage.deletesitem
local verilebilir = true
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.KeyDown:connect(function(key)
	
	key = key:lower()
	
	if key == "f" and verilebilir == true then
		saladEvent:FireServer(weapon)
		local tool = game.Players.LocalPlayer.Backpack:WaitForChild("Lantern")
		game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"):EquipTool(tool)
		saladEvent2:FireServer(tool)
		verilebilir = false
		print("Servera iletildi")
		
	elseif verilebilir == false then
		local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("Lantern")
		dleteitem:FireServer(tool)
		print("item silindi")
		verilebilir = true
	end
end)

Thanks for help :slightly_smiling_face:

I am not sure what the issue is, but I noticed 2 things.

First, elseif verilebilir == false then should be elseif key == "f" and verilebilir == false then. Currently, the dleteitem event is fired no matter what the input key is.

Second, there is no need to send the tool through the remote event. You can just get the tool from the server script. This may be causing the issue, however, I am not sure.

1 Like

I tried what you said but it prints an error at line 12
image
image

If that’s the case, do

if tool then tool:Destroy() end

1 Like

Its now deleted but I can see it even though it’s been deleted
Edit: i added game.Players.LocalPlayer.Character.Humanoid:UnequipTools() to the client script now it works well!