Equip Button takes two clicks

Hi! So I made an inventory system but there are still a few bugs. The main one I am trying to fix is that when there are at least two items inside the inventory frame and both of them are equipped, it takes two clicks to unequip them. This is a huge problem because by the time you finally change the button to “equip” the item is still in the players backpack. Here is the code:

function updateInventory()

	for i, child in pairs(invFrame.ShopGUI.Tools:GetChildren()) do
		if child:IsA("ImageLabel") then child:Destroy() end
	end


	local ownedTrails = ownedTrailsFolder:GetChildren()
	print(ownedTrails)

	table.sort(ownedTrails, function(a, b)
		return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
	end)


	for i, tool in pairs(ownedTrails) do
		local item = script.InvItem:Clone()
		item.Image = tool:WaitForChild("Folder").ImageLabel.Image
		item.SelectButton.Text = "Equip"
		item.Title.Text = tool.Name
		item.Visible = true



		item.Parent = invFrame.ShopGUI.Tools






		item.SelectButton.MouseButton1Click:Connect(function()
			local price = tool.Price.Value
			local coins = player.leaderstats.Coins.Value
			if item == SelectedTrail then
				item.SelectButton.Text = "Equip"

				SelectedTrail = nil
			else
				if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
					item.SelectButton.Text = "Unequip" 
				else
					item.SelectButton.Text = "Unequip"
				end


				SelectedTrail = item
			end

			TrailSelectedRE:FireServer(tool)
		end)
	end




end		

People in other posts said that I need to change SelectedTrail to a table to store multiple values. But how would I add that to the script and modify it so when the event is triggered it still sends over one tool name and not a whole table?

Still having this issue and I don’t know where to start.

Hi Rome.

Because you have a connection inside a function.

Equip function should be outside

item.SelectButton.MouseButton1Click:Connect(function() end)

or maybe debounce/variables or values problem.

Im confused by what you mean. Do you mind editing the script above to show?

1 Like

wait is updateInventory()

runs when u play the game or press a button?

anyway try to put this down the function:

updateInventory()

without events like mouse click or button click
and see if it’s working

Like:

function updateInventory()

	for i, child in pairs(invFrame.ShopGUI.Tools:GetChildren()) do
		if child:IsA("ImageLabel") then child:Destroy() end
	end


	local ownedTrails = ownedTrailsFolder:GetChildren()
	print(ownedTrails)

	table.sort(ownedTrails, function(a, b)
		return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
	end)


	for i, tool in pairs(ownedTrails) do
		local item = script.InvItem:Clone()
		item.Image = tool:WaitForChild("Folder").ImageLabel.Image
		item.SelectButton.Text = "Equip"
		item.Title.Text = tool.Name
		item.Visible = true



		item.Parent = invFrame.ShopGUI.Tools






		item.SelectButton.MouseButton1Click:Connect(function()
			local price = tool.Price.Value
			local coins = player.leaderstats.Coins.Value
			if item == SelectedTrail then
				item.SelectButton.Text = "Equip"

				SelectedTrail = nil
			else
				if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
					item.SelectButton.Text = "Unequip" 
				else
					item.SelectButton.Text = "Unequip"
				end


				SelectedTrail = item
			end

			TrailSelectedRE:FireServer(tool)
		end)
	end




end

-----Luffy edits:
updateInventory() -- call it when a game start to update it or when player character added
1 Like

I already have it so updateInventory() runs when a character is added or if there is a new child in the ownedTools folder so thats not the issue.

I suggest you when u press equip button fireserver ok… so
from serverside you should check :

Serverscript:

--EXAMPLE:

--Serverscript:
TrailSelectedRE.OnServerEvent:Connect(function(plr)
  if playerhavetool then -- we checked the player have the tool and we want to unequip it
  TrailSelectedRE:FireClient(plr,"test",false) --unequipped
  --Destroying or clone or ur stuff
  elseif not playerhavetool then
  TrailSelectedRE:FireClient(plr,"test",true) --equipped
  --clone the tool to the player
  end
end)

Local Script :

--Localscript
TrailSelectedRE.OnClientEvent:Connect(function(toolname,value)
if toolname == "test" and value == true then --Check the thing name and if it's true
textbutton.Text = "Unequip"
elseif toolname == "test" and value == false then--Check the thing name and if it's false
textbutton.Text = "Equip"
end
end)

I already have all of it. Everything works fine in the inventory. Its just it takes two clicks of the “unequip” button to actually change the text buttons text back to “equip”.

maybe because u didn’t change the name of “item” the cloned one to the tool.Name

or maybe

SelectedTrail = item

TrailSelectedRE:FireServer(tool) | and u fired the tool instead of item? idk

read ur script and check it, Good luck hope u find the solution!

Nothing of what you said was of use to me but thank you for your help.