Issue with trail shop

I’ve created a trail shop of which you can buy trails from for an in-game currency, but implementing it has been very tough. After spending a long time trying to map it out correctly, it still hasn’t turned out the way I want it to.

I think the issue is that I’m disconnecting the anonymous functions before they can run, because I’m not sure if they yield the script or not. But when I remove them, it keeps on running the same function that I want to disconnect from continuously. If this is confusing, then let me explain with some chunks of my code.

for i, v in pairs(buttons) do
	v.MouseButton1Click:Connect(function()
		if v.IsOwned.Value and not v.Name == equipped then
			previewText.Text = "You Already Own This (Unequipped)"
			actionButton.Text = "Equip Trail"
		local cn=	actionButton.MouseButton1Click:Connect(function()
				game.ReplicatedStorage.Equip:FireServer(v.Name)
				equipped = v.Name
				if v.UIGradient then
					game.ReplicatedStorage.SendEquipped:FireServer(equipped,v.BackgroundColor3, true, v.UIGradient.Color)
				else
					game.ReplicatedStorage.SendEquipped:FireServer(equipped,v.BackgroundColor3, false, nil)

				end

				previewText.Text = "You currently have this equipped"
				actionButton.Text = "Unequip Trail"
			end)
			cn:Disconnect()
		elseif v.IsOwned.Value and v.Name == equipped then
			previewText.Text = "This Is Currently equipped"
			actionButton.Text = "Unequip"
			local connection= 	actionButton.MouseButton1Click:Connect(function()
				game.ReplicatedStorage.Unequip:FireServer()
				previewText.Text = "You Already Own This (Unequipped)"
				actionButton.Text = "Equip Trail"
			end)
			connection:Disconnect()

		else
			previewText.Text = "Do you want to buy this for "..v.Price.Value.." Spleef Coins?"
			actionButton.Text= "Confirm Purchase"
			local cn = actionButton.MouseButton1Click:Connect(function()	
				game.ReplicatedStorage.ProcessPurchase:FireServer(v.Name, v.Price.Value)
				table.insert(Owned, #Owned+1, v.Name)
				game.ReplicatedStorage.SendOwned:FireServer(Owned)
				previewText.Text = "You Already Own This (Unequipped)"
				actionButton.Text = "Equip Trail"


			end)
			cn:Disconnect()



		end
	end)
end

This is the button handler code. It handles actions when the player selects one of the buttons within the table of buttons.

As you can see, i have 3 conditionals.

One handles unequipping, another equipping and the last, purchasing.

The issue is, within each of the conditionals. I face a problem.

The MouseButton1Click functions within these conditionals won’t stop running this function:

	local cn = actionButton.MouseButton1Click:Connect(function()	
				game.ReplicatedStorage.ProcessPurchase:FireServer(v.Name, v.Price.Value)
				table.insert(Owned, #Owned+1, v.Name)
				game.ReplicatedStorage.SendOwned:FireServer(Owned)
				previewText.Text = "You Already Own This (Unequipped)"
				actionButton.Text = "Equip Trail"


			end)
			cn:Disconnect()

Meaning despite me owning it, and repeatedly trying to equip it, the text stays the same.

I tried disconnecting it, with no success, removing disconnect doesn’t help either. All i want, is when it’s been purchased, for it to then automatically, move to the equipping conditional, instead of yielding on that one function.

I’m so sorry if you don’t understand. It’s an issue that will prove confusing to some people

Try adding a debounce and that might help

1 Like

Nevermind I know what you mean. I’ll try that rn

Unfortunately, no difference. Current code:

	if not db then
				db = true
				cn=	actionButton.MouseButton1Click:Connect(function()
					game.ReplicatedStorage.Equip:FireServer(v.Name)
					equipped = v.Name
					if v.UIGradient then
						game.ReplicatedStorage.SendEquipped:FireServer(equipped,v.BackgroundColor3, true, v.UIGradient.Color)
					else
						game.ReplicatedStorage.SendEquipped:FireServer(equipped,v.BackgroundColor3, false, nil)

					end

					previewText.Text = "You currently have this equipped"
					actionButton.Text = "Unequip Trail"

				end)
			else 
				cn:Disconnect()
				db = false
			end


		elseif v.IsOwned.Value and v.Name == equipped then
			local cn
			local db = false
			previewText.Text = "This Is Currently equipped"
			actionButton.Text = "Unequip"
			if  not db then
				db = true
				cn = actionButton.MouseButton1Click:Connect(function()
					game.ReplicatedStorage.Unequip:FireServer()
					previewText.Text = "You Already Own This (Unequipped)"
					actionButton.Text = "Equip Trail"
				end)
			else
				cn:Disconnect()
				db = false
			end



		else
			local db = false
			local cn
			previewText.Text = "Do you want to buy this for "..v.Price.Value.." Spleef Coins?"
			actionButton.Text= "Confirm Purchase"
			if not db then	
				db = true

				cn = actionButton.MouseButton1Click:Connect(function()
					game.ReplicatedStorage.ProcessPurchase:FireServer(v.Name, v.Price.Value)
					table.insert(Owned, #Owned+1, v.Name)
					game.ReplicatedStorage.SendOwned:FireServer(Owned)
					previewText.Text = "You Already Own This (Unequipped)"
					actionButton.Text = "Equip Trail"
				end)
			else
				cn:Disconnect()
				db  = false

			end



		end