Issue with my shop

  1. What do you want to achieve? I have a shop where you can equip and unequip items, but i have an issue.

  2. What is the issue? When i first join the game, and equip or unequip an item, it work on first time and on the second time it dosen’t work, it stays on unequip even tho i press the unequip button again, this is the local script:

function UpdateShop(items)
	print(items)
	for i,v in pairs(ShopConnections) do
		v:Disconnect()
	end
	for i,v in pairs(Shop.ItemsList.ScrollingFrame:GetChildren()) do
		if v:IsA("Frame") and v.Name ~= "Template" then
			v:Destroy()
		end
	end
	for i,v in pairs(Shop.Informations:GetChildren()) do
		if v:IsA("TextButton") and v.Name == "BuyButton" then
			v:Destroy()
		end
	end
	Shop.Informations.ItemName.Text = "Select an Item"
	Shop.Informations.ItemIcon.Visible = false
	Shop.Informations.ItemIcon.Limited.Visible = false
	for i,v in pairs(items) do
		local TemplateClone = Shop.ItemsList.ScrollingFrame.Template:Clone()
		TemplateClone.Price.Text = v.Price
		TemplateClone.Parent = Shop.ItemsList.ScrollingFrame
		TemplateClone.Name = i
		print(v)
		if v.Owned then
			TemplateClone.UIStroke.Color = Color3.fromRGB(38, 255, 0)
		end
		TemplateClone.Visible = true
		ShopConnections[i] = TemplateClone.Button.MouseButton1Click:Connect(function()
			Shop.Informations.ItemName.Text = i
			Shop.Informations.ItemIcon.Visible = true
			for i,v in pairs(Shop.Informations:GetChildren()) do
				if v:IsA("TextButton") and v.Name == "BuyButton" then
					v:Destroy()
				end
			end
			local BuyButtonClone = Shop.Informations.BuyButtonTemplate:Clone()
			BuyButtonClone.Parent = Shop.Informations
			BuyButtonClone.Name = "BuyButton"
			if v.Owned then
				if v.Equipped then
					Shop.Informations.BuyButton.Text = "Unequip"
					Shop.Informations.BuyButton.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
					BuyButtonClone.MouseButton1Click:Connect(function()
						ReplicatedStorage.Client.Shop.EquipShopItem:FireServer("Nametags", i, false)
						UpdateShop(ReplicatedStorage.Client.Shop.GetShopItems:InvokeServer("Nametags"))
					end)
				else
					Shop.Informations.BuyButton.Text = "Equip"
					Shop.Informations.BuyButton.BackgroundColor3 = Color3.fromRGB(0, 110, 255)
					BuyButtonClone.MouseButton1Click:Connect(function()
						ReplicatedStorage.Client.Shop.EquipShopItem:FireServer("Nametags", i, true)
						UpdateShop(ReplicatedStorage.Client.Shop.GetShopItems:InvokeServer("Nametags"))
					end)
				end
			else
				Shop.Informations.BuyButton.Text = "Buy (" .. v.Price .. ")"
				Shop.Informations.BuyButton.BackgroundColor3 = Color3.fromRGB(82, 206, 0)
				BuyButtonClone.MouseButton1Click:Connect(function()
					ReplicatedStorage.Client.Shop.BuyShopItem:FireServer("Nametags", i)
					UpdateShop(ReplicatedStorage.Client.Shop.GetShopItems:InvokeServer("Nametags"))
				end)
			end
			if v.Limited == true then
				Shop.Informations.ItemIcon.Limited.Visible = true
			else
				Shop.Informations.ItemIcon.Limited.Visible = false
			end
			Shop.Informations.BuyButton.Visible = true
		end)
	end
end

(The issue comes from the local side, i checked the server side and it works)

  1. What solutions have you tried so far? I tried to fix it myself but i can’t find the issue
3 Likes

you should disconnect the previous button’s click event before creating a new button. Here’s how you can modify your script:

-- Add this line at the top of your script
local buyButtonConnection

-- Then, before creating a new BuyButtonClone, disconnect the previous button's click event
if buyButtonConnection then
	buyButtonConnection:Disconnect()
end

local BuyButtonClone = Shop.Informations.BuyButtonTemplate:Clone()
BuyButtonClone.Parent = Shop.Informations
BuyButtonClone.Name = "BuyButton"

-- Then, when connecting the new button's click event, store the connection in buyButtonConnection
if v.Owned then
	if v.Equipped then
		Shop.Informations.BuyButton.Text = "Unequip"
		Shop.Informations.BuyButton.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
		buyButtonConnection = BuyButtonClone.MouseButton1Click:Connect(function()
			ReplicatedStorage.Client.Shop.EquipShopItem:FireServer("Nametags", i, false)
			UpdateShop(ReplicatedStorage.Client.Shop.GetShopItems:InvokeServer("Nametags"))
		end)
	else
		Shop.Informations.BuyButton.Text = "Equip"
		Shop.Informations.BuyButton.BackgroundColor3 = Color3.fromRGB(0, 110, 255)
		buyButtonConnection = BuyButtonClone.MouseButton1Click:Connect(function()
			ReplicatedStorage.Client.Shop.EquipShopItem:FireServer("Nametags", i, true)
			UpdateShop(ReplicatedStorage.Client.Shop.GetShopItems:InvokeServer("Nametags"))
		end)
	end
else
	Shop.Informations.BuyButton.Text = "Buy (" .. v.Price .. ")"
	Shop.Informations.BuyButton.BackgroundColor3 = Color3.fromRGB(82, 206, 0)
	buyButtonConnection = BuyButtonClone.MouseButton1Click:Connect(function()
		ReplicatedStorage.Client.Shop.BuyShopItem:FireServer("Nametags", i)
		UpdateShop(ReplicatedStorage.Client.Shop.Get:ItemsShopInvokeServer("Nametags"))
	end)
end

Let me know if you need help with this, this was also just directly added to your current script.

i put this and it still stays on “unequip” after i press unequip