Tool being duplicated after clicking two different buttons

Basically, when I try to click one button, it gives me the tool, but then if I click another tool or click the same tool twice, it duplicates it, even tho it shall be destroying the tool rather than multiplying it, any help would be good.

You can do and if statement that returns to end if the tool is not nil.
Something like this:
if ToolName ~= nil then return end
This should prevent from duplicating.

image I did what you said me to do, but it still duplicates.

Oh I think you placed it in the wrong area so it would be something like:

Player.Backpack.Axe:Destroy()
if not Player.Backpack:FindFirstChild("Axe") then
--you give them the object
end

This is my code, if your wondering, but I still can’t figure it out.

Oh you didn’t add a debounce so it would keep running through if the item is equipped. So to fix this do

 local debounce = false
AxeEvent.OnServerEvent:Connect(function(Player,Frame)
isEquipped = not isEquipped
if isEquiooed and not Deboucne then
Debounce = true
--Code
wait(1)
Deboucne = false
--Code

Still having the same problem.
image

Here is the updated code.

Okay if not this I’m at a bust sorry. Try removing the wait(1) and debounce = false because it’s already setting at false at the beginning.

Couple things I think should be changed:

Declare your Debounce variable outside of the OnServerEvent:Connect function. Currently, it is declared within the function so it will always be false every time the event happens, so it currently has no effect.

The isEquipped logic looks backwards. It should be more like:

   if not isEquipped and not Debounce then
     -- Equip tool
   elseif isEquipped ...
     -- Unequip tool
   end

Another possibility is that it’s trying to compare the object addresses for the Color3 objects, versus comparing RBG values of the Color3 objects. To fix this, you could either get the values of the Frame’s BackgroundColor3 and compare them each separately, or you may be able to declare a Color3 object outside of the function, and use that Color3 object to assign and compare Frame.BackgroundColor3 within the function. The second option would be better, if it works as intended. Actually, you probably don’t even need to check the color of the Frame.BackgroundColor3 in the elseif-check and it should still behave the same.

I’m not sure if those things alone will fix the problem or if there’s more issues. It’s always more difficult to analyze conditional statements when each test has multiple conditions, e.g. if A and B, elseif X and Y and Z… it won’t hurt performance to nest if-statements once in a while.

Btw, it would be easier for people to help offer you code modifications if you paste your code as text instead of screenshots. That way, people can copy and paste and edit your code instead of typing it all from scratch.

I have started using a different way, but I’m now having problems destroying them, and i’m still wondering what the issue is since I just copied the code I wrote for the old way I used, and it wouldn’t work.

local Tool = game:GetService("ReplicatedStorage").Tool
Tool.OnServerEvent:Connect(function(Player, Tool, Frame)
	isEquipped = not isEquipped 
	local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(Tool):Clone()
	if isEquipped then	
		UndefinedTool.Parent = Player.Backpack
		Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
	elseif isEquipped and UndefinedTool then
		Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
		UndefinedTool:Destroy()
		UndefinedTool = nil
	end	
end)

Try this:

local int = 0
int += 1
if int >1 then
local oldaxe = player.Backpack:FindFirstChild("Axe")
if oldaxe then
oldaxe:Destroy()
end
end

There might be some mismatches going on. It looks like Tool is a RemoteEvent, but it’s also a tool? Plus, if you try to pass an object between client and server, I don’t think the receiver can try to look for the object from its address, which is what I think you’re trying to do.

Also, the isEquipped logic still looks backwards.

Try something like this to see if it works better… I’m not sure if you had a RemoteEvent and a tool with the same name, so I renamed what I think was an event to EquipToolEvent. I also changed where and how isEquipped is assigned, and some other misc changes.

local EquipToolEvent = game:GetService("ReplicatedStorage").EquipToolEvent
EquipToolEvent.OnServerEvent:Connect(function(Player, Tool, Frame)
  local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(Tool.Name):Clone()
  if not isEquipped then	
    -- Equip tool
    UndefinedTool.Parent = Player.Backpack
    Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
    isEquipped = true
  else
    -- Unequip tool
    if UndefinedTool then
      Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
      UndefinedTool:Destroy()
      UndefinedTool = nil
      isEquipped = false
    end
  end	
end)

It seems like it still duplicates the tool :frowning:
image

Also the code you passed me over.

It just seems like the script isn’t going past the else statement, which is really confusing.

It’s not going into the else statement bc of this line:

local isEquipped = false

Also, I think what I said about the RemoveEvent just added confusion, so try this instead. Note that it’s referring to Tool.Name in the WaitForChild call.

local Tool = game:GetService("ReplicatedStorage").Tool
Tool.OnServerEvent:Connect(function(Player, Tool, Frame)
  local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(Tool.Name):Clone()
  if not isEquipped then	
    -- Equip tool
    UndefinedTool.Parent = Player.Backpack
    Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
    isEquipped = true
  else
    -- Unequip tool
    if UndefinedTool then
      Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
      UndefinedTool:Destroy()
      UndefinedTool = nil
      isEquipped = false
    end
  end	
end)

Yeah but, i’m sending the name of the tool to the server, as a string.

And also, what is isEquipped supposed to be then? since its not a variable nor defined to anything.

Ah, okay. Probably better to name the arg something like ToolName then to avoid confusion.