Equip/Unequip tool button doesn't work

Oh, it’s very intristing !

The equip and unequip work perfectly now.
But i’m facing a new problem.

When I click a tool, then press equip it works, then if I press unequip it works. But if i choose a new tool, it is stuck on the first tool.

Like if I equip tool 1 then unequip or re-equip it, it works and then if i equip tool2 it equips tool1 :confused:

This is how my script works, get all plr’s tools, create a new template for each one, and if a template is clicked, make a gui appears, change all gui’s informations to tool info like name etc… and then if equipbutton is pressed:

equipButton.MouseButton1Click:Connect(function()
				local equipped = tool:WaitForChild("Equipped").Value
				if not equipped then
					game.ReplicatedStorage.Remotes.EquipTool:FireServer(tool.Name)
					equipButton.Text = "Unequip"
					equipButton.BackgroundColor3 = Color3.fromRGB(165, 40, 9)
					equipButton.BorderColor3 = Color3.fromRGB(126, 25, 0)
				else
					game.ReplicatedStorage.Remotes.UnequipTool:FireServer(tool.Name)
					equipButton.Text = "Equip"
					equipButton.BackgroundColor3 = Color3.fromRGB(0, 193, 0)
					equipButton.BorderColor3 = Color3.fromRGB(0, 85, 0)
				end
			end)

this is the entire script without function wich sets the informations of the tool :

local Inventory = script.Parent
local frame = Inventory:WaitForChild("Frame")
local iFrame = Inventory:WaitForChild("IFrame")
local SFrame = frame:WaitForChild("ScrollingFrame")
local template = SFrame:WaitForChild("Template")
local desciption = iFrame:WaitForChild("Description")
local displayItem = iFrame:WaitForChild("DisplayItem")
local objectName = iFrame:WaitForChild("ObjectName")
local frameOpen = frame:WaitForChild("Open")
local iFrameOpen = iFrame:WaitForChild("Open")
local equipButton = iFrame:WaitForChild("EquipButton")
local ToolsOnglet = frame:WaitForChild("ToolsOnglet")
local BackpacksOnglet = frame:WaitForChild("BackpacksOnglet")
local PetsOnglet = frame:WaitForChild("PetsOnglet")

local module = require(Inventory:WaitForChild("GuiHolder"))

local function toolsDisplay()
	for _, temp in pairs(SFrame:GetChildren()) do
		if temp:IsA("TextButton") then
			if temp.Name ~= "Template" then
				temp:Destroy()
			end
		end
	end
	
	local plr = game.Players.LocalPlayer
	
	for _, tool in pairs(plr.Inventory.Tools:GetChildren()) do
		local newTemplate = template:Clone()
		newTemplate.Name = tool.Name
		newTemplate.ObjectName.Text = tool.Name
		newTemplate.Visible = true
		newTemplate.Parent = SFrame
		
		local object = tool:Clone()
		object.Parent = newTemplate.VPF
		
		local cam = Instance.new("Camera")
		cam.CFrame = CFrame.new(object.Handle.Position + (object.Handle.CFrame.lookVector*5),object.Handle.Position)
		cam.Parent = newTemplate.VPF
		newTemplate.VPF.CurrentCamera = cam
		
		---- If player click an item (template) ----
		newTemplate.MouseButton1Click:Connect(function()
			
			local function SetupIFrame()
				desciption.Text = tool:WaitForChild("Description").Value
				objectName.Text = tool.Name
				local rarity = tool:FindFirstChild("Rarity").Value
				if rarity == "Common" then
					objectName.TextColor3 = Color3.fromRGB(56, 56, 56)
				elseif rarity == "Uncommon" then
					objectName.TextColor3 = Color3.fromRGB(95, 180, 76)
				elseif rarity == "Rare" then
					objectName.TextColor3 = Color3.fromRGB(18, 59, 239)
				elseif rarity == "Epic" then
					objectName.TextColor3 = Color3.fromRGB(126, 0, 189)
				elseif rarity == "Legendary" then
					objectName.TextColor3 = Color3.fromRGB(225, 49, 25)
				elseif rarity == "Secret" then
					objectName.TextColor3 = Color3.fromRGB(22, 2, 83)
				else
					-- If any rarity is found then --
					objectName.Text = "There was an error!"
				end
				
				for _, v in pairs(displayItem.VPF:GetChildren()) do
					if v then
						v:Destroy()
					end
				end
				
				local object2 = tool:Clone()
				object2.Parent = displayItem.VPF
				
				local cam2 = Instance.new("Camera")
				cam2.CFrame = CFrame.new(object2.Handle.Position + (object2.Handle.CFrame.lookVector*5), object2.Handle.Position)
				cam2.Parent = displayItem.VPF
				displayItem.VPF.CurrentCamera = cam2
				
				local equipped = tool:WaitForChild("Equipped")
				
				if tool.Unequipped then
					equipButton.Text = "Equip"
					equipButton.BackgroundColor3 = Color3.fromRGB(0, 193, 0)
					equipButton.BorderColor3 = Color3.fromRGB(0, 85, 0)
				else
					equipButton.Text = "Unequip"
					equipButton.BackgroundColor3 = Color3.fromRGB(165, 40, 9)
					equipButton.BorderColor3 = Color3.fromRGB(126, 25, 0)
				end
			end -- End of the function --
			
			if not iFrameOpen.Value then
				SetupIFrame()
				module.OpenIFrame()
				iFrameOpen.Value = true
			else
				module.CloseIFrame()
				wait(1.2)
				SetupIFrame()
				module.OpenIFrame()
			end

			equipButton.MouseButton1Click:Connect(function()
				local equipped = tool:WaitForChild("Equipped").Value
				if not equipped then
					game.ReplicatedStorage.Remotes.EquipTool:FireServer(tool.Name)
					equipButton.Text = "Unequip"
					equipButton.BackgroundColor3 = Color3.fromRGB(165, 40, 9)
					equipButton.BorderColor3 = Color3.fromRGB(126, 25, 0)
				else
					game.ReplicatedStorage.Remotes.UnequipTool:FireServer(tool.Name)
					equipButton.Text = "Equip"
					equipButton.BackgroundColor3 = Color3.fromRGB(0, 193, 0)
					equipButton.BorderColor3 = Color3.fromRGB(0, 85, 0)
				end
			end)
		end)
	end
end

ToolsOnglet.ToolImage.MouseButton1Click:Connect(function()
	toolsDisplay()
end)

Do you know why it is stuck?

Oh the problem come from this script, where i send the toolName to the server script

so it works now

30char

The equip and unequip works but it is stuck on the first tool i equip :confused:

what did you meant by this? the unequipping does not work?

Imagine you have two tools in your inventory.

You equip the first one, then you can unequip it, equip it like you want, (it is perfectly working).
But now you want to equip the second tool.
So you unequip the first one (working) and now you go on your second tool, press equip but what it happens is equip the tool one.

Just skimming your code, it could be because you’re using the same equipButton to bind a function on MouseButton1Click event more than one time without disconnecting the previous.

equipButton.MouseButton1Click:Connect(function()
				local equipped = tool:WaitForChild("Equipped").Value
				if not equipped then
					game.ReplicatedStorage.Remotes.EquipTool:FireServer(tool.Name)
					equipButton.Text = "Unequip"
					equipButton.BackgroundColor3 = Color3.fromRGB(165, 40, 9)
					equipButton.BorderColor3 = Color3.fromRGB(126, 25, 0)
				else
					game.ReplicatedStorage.Remotes.UnequipTool:FireServer(tool.Name)
					equipButton.Text = "Equip"
					equipButton.BackgroundColor3 = Color3.fromRGB(0, 193, 0)
					equipButton.BorderColor3 = Color3.fromRGB(0, 85, 0)
				end
			end)

^doing this multiple times will stack the functions and cause the version for tool1 and tool2 to run. You need to check if there’s an event connected and disconnect it based on that. Or just do some simple debounce like

if connectedev == true then equipButton.MouseButton1Click:disconnect() end
1 Like

That’s so strange!
Let me show you with screenshots.

I put a print (tool.Name) when you click equipbutton and look what it does:

Without clicking :

after clicking equipbutton :

And after clicking equip on another tool, you can see it prints the toolName and after the name of the last items…
It looks like saving the last the name of all my tools after clicking equip it…

Mhh what does it mean : connectdev? :c

And where should I put this line of code?

You can call it debounce, it’s just a variable. Put it outside of the code at the top where you define variables like equipButton.
So:

local Inventory = script.Parent
local frame = Inventory:WaitForChild("Frame")
local iFrame = Inventory:WaitForChild("IFrame")
local SFrame = frame:WaitForChild("ScrollingFrame")
local template = SFrame:WaitForChild("Template")
local desciption = iFrame:WaitForChild("Description")
local displayItem = iFrame:WaitForChild("DisplayItem")
local objectName = iFrame:WaitForChild("ObjectName")
local frameOpen = frame:WaitForChild("Open")
local iFrameOpen = iFrame:WaitForChild("Open")
local equipButton = iFrame:WaitForChild("EquipButton")
local ToolsOnglet = frame:WaitForChild("ToolsOnglet")
local BackpacksOnglet = frame:WaitForChild("BackpacksOnglet")
local PetsOnglet = frame:WaitForChild("PetsOnglet")
local debounce = false

Then in the equipButton event, make it true and add a line code before that to make sure to disconnect the previous.

	if debounce == true then equipButton.MouseButton1Click:disconnect() debounce = false end
	equipButton.MouseButton1Click:Connect(function()
				local debounce = true
				local equipped = tool:WaitForChild("Equipped").Value
				if not equipped then
					game.ReplicatedStorage.Remotes.EquipTool:FireServer(tool.Name)
					equipButton.Text = "Unequip"
					equipButton.BackgroundColor3 = Color3.fromRGB(165, 40, 9)
					equipButton.BorderColor3 = Color3.fromRGB(126, 25, 0)
				else
					game.ReplicatedStorage.Remotes.UnequipTool:FireServer(tool.Name)
					equipButton.Text = "Equip"
					equipButton.BackgroundColor3 = Color3.fromRGB(0, 193, 0)
					equipButton.BorderColor3 = Color3.fromRGB(0, 85, 0)
				end
			end)

I hope I solved your problem

1 Like

Mhh, if I’m right debounce is always egual to true excepted the first time the code run

I fixed the code so that when it disconnects it also sets debounce to false right after

1 Like

My bad I didn’t see the debounce = false after
x’)

It is very strange, I put the debounce in my variable and change the script to :

if debounce == true then equipButton.MouseButton1Click:disconnect() debounce = false end
			equipButton.MouseButton1Click:Connect(function()
				local debounce = true
				print(tool.Name)
				local equipped = tool:WaitForChild("Equipped").Value
				if not equipped then
					game.ReplicatedStorage.Remotes.EquipTool:FireServer(tool.Name)
					equipButton.Text = "Unequip"
					equipButton.BackgroundColor3 = Color3.fromRGB(165, 40, 9)
					equipButton.BorderColor3 = Color3.fromRGB(126, 25, 0)
				else
					game.ReplicatedStorage.Remotes.UnequipTool:FireServer(tool.Name)
					equipButton.Text = "Equip"
					equipButton.BackgroundColor3 = Color3.fromRGB(0, 193, 0)
					equipButton.BorderColor3 = Color3.fromRGB(0, 85, 0)
				end
			end)

but i still have the same problem…

I think it’s because inside the MouseButton1Click I accidentally put local debounce = true, should be simply debounce = true. Also I did that the deprecated way. Remove debounce from the code and replace it with something called ev. ev stands for event of course

local Inventory = script.Parent
local frame = Inventory:WaitForChild("Frame")
local iFrame = Inventory:WaitForChild("IFrame")
local SFrame = frame:WaitForChild("ScrollingFrame")
local template = SFrame:WaitForChild("Template")
local desciption = iFrame:WaitForChild("Description")
local displayItem = iFrame:WaitForChild("DisplayItem")
local objectName = iFrame:WaitForChild("ObjectName")
local frameOpen = frame:WaitForChild("Open")
local iFrameOpen = iFrame:WaitForChild("Open")
local equipButton = iFrame:WaitForChild("EquipButton")
local ToolsOnglet = frame:WaitForChild("ToolsOnglet")
local BackpacksOnglet = frame:WaitForChild("BackpacksOnglet")
local PetsOnglet = frame:WaitForChild("PetsOnglet")
local ev; --Automatically set to nil

Then do

	if ev then ev:Disconnect() end
	ev = equipButton.MouseButton1Click:Connect(function()
				local equipped = tool:WaitForChild("Equipped").Value
				if not equipped then
					game.ReplicatedStorage.Remotes.EquipTool:FireServer(tool.Name)
					equipButton.Text = "Unequip"
					equipButton.BackgroundColor3 = Color3.fromRGB(165, 40, 9)
					equipButton.BorderColor3 = Color3.fromRGB(126, 25, 0)
				else
					game.ReplicatedStorage.Remotes.UnequipTool:FireServer(tool.Name)
					equipButton.Text = "Equip"
					equipButton.BackgroundColor3 = Color3.fromRGB(0, 193, 0)
					equipButton.BorderColor3 = Color3.fromRGB(0, 85, 0)
				end
			end)

I really hope we solved the problem this time.

1 Like

It’s working !

Omg thx you so much.

I’m almost to cry, i’m so happy!
I past so much hours on this stupid inventory!

Thank you so muccccccccccccchhh…

I never been happy like this since I downloaded Roblox x’)

Thx you thx you thx you !

1 Like