How do you make this unequip the tool when you click the gui button again?

So currently it equips the torch when you press the gui button, but I don’t know how to make it unequip it after you click it again. Can someone help please?

local player = game.Players.LocalPlayer

local tool = player:WaitForChild(“Backpack”):WaitForChild(“Torch”)

script.Parent.MouseButton1Down:connect(function()

local wep = player.Character:FindFirstChild(tool)

if player.Character then

if wep == nil then

player.Character.Humanoid:EquipTool(tool)

elseif wep ~= nil then

wep.Parent = player.Backpack

end

end

end)

1 Like

Here:

local player = game.Players.LocalPlayer

local tool = player:WaitForChild(“Backpack”):WaitForChild(“Torch”)

script.Parent.MouseButton1Down:connect(function()

local wep = player.Character:FindFirstChild(tool)

if player.Character then

if wep == nil then

player.Character.Humanoid:EquipTool(tool)

elseif wep ~= nil then

wep.Parent = player.Backpack

end

end

end)

It seems to be fine? Are there any errors in the output?

I know it works, but I don’t know how to make it unequip the tool after you click the button again

I don’t think using EquipTool in the client side is good, as it would create replication issues.

local player = game.Players.LocalPlayer

local tool = player:WaitForChild("Backpack"):WaitForChild("Torch")
local equipped = false

script.Parent.MouseButton1Click:connect(function()
	equipped = not equipped
	if equipped then	
		player.Character.Humanoid:EquipTool(tool)
	else
		player.Character.Humanoid:UnequipTools()
	end
end)

Here’s a gif of me clicking the button.
https://gyazo.com/62f7c1f9bf052addb99c93f08db00cce

Thanks it worked!!!