Need help with disconnecting older connections

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    When the player wants to equip and item, it’ll only give them one of that item instead of multiple of the same item.

  2. What is the issue? Include screenshots / videos if possible!
    If the player were to switch back and forth items in the shop, it’ll add new connections which would then give the player multiple of the same item if they were to equip an item.

https://streamable.com/bw8sae

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried making the nameVal:Changed function a connection to try and disconnect older connections but couldn’t figure out how to and where to place the :Disconnect events. I also tried to see if anyone had the same problem as me on the DevForums but nothing similar.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- Services
local RS = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

-- REs
local REFolder = RS:WaitForChild("RemoteEvents").shopSystem

-- Variables
local player = players.LocalPlayer

local mainFrame = script.Parent.Parent

local frames = mainFrame.Frames

local homeFrame = frames:FindFirstChild("1_homeFrame")
local gunsFrame = frames:FindFirstChild("2_gunsFrame")
local meleeFrame = frames:FindFirstChild("3_meleeFrame")
local miscFrame = frames:FindFirstChild("4_miscFrame")

local infoFrame = mainFrame:FindFirstChild("infoFrame")

local buyButton = infoFrame.buyButton

-- Values
local nameVal = infoFrame.nameValue
local priceVal = infoFrame.priceValue

-- Local functions
local function clearViewFrame()
	for i, v in pairs(infoFrame.itemViewFrame:GetDescendants()) do
		if v:IsA("ViewportFrame") then
			v:Destroy()
		end
	end
end

local function updateItems(frame)
	local pOI = player:WaitForChild("OwnedItems")
	
	pOI.ChildAdded:Connect(function(newItem)
		warn("Item: ".. newItem.Name .." || Frame: ".. frame.Name)
		for i, v in pairs(frame:GetDescendants()) do
			if v:IsA("TextLabel") and v.Name == "itemPrice" then
				local item = v.Parent
				
				if newItem.Name == item.itemName.Text then
					local priceLabel = item.itemPrice
					priceLabel.Text = "OWNED"
					priceLabel.TextColor3 = Color3.fromRGB(75, 255, 75)
					priceLabel.UIStroke.Color = Color3.fromRGB(80, 175, 80)
				end
			end
		end
	end)
end

local function updateBuyButton()
	if player:WaitForChild("OwnedItems"):FindFirstChild(nameVal.Value) then
		if not (player.Backpack:FindFirstChild(nameVal.Value) or player.Character:FindFirstChild(nameVal.Value)) then
			buyButton.Text = "EQUIP"
			buyButton.BackgroundColor3 = Color3.fromRGB(100, 255, 145)
			buyButton.borderStroke.Color = Color3.fromRGB(49, 125, 69)
		elseif player.Backpack:FindFirstChild(nameVal.Value) or player.Character:FindFirstChild(nameVal.Value) then
			buyButton.Text = "UNEQUIP"
			buyButton.BackgroundColor3 = Color3.fromRGB(255, 165, 110)
			buyButton.borderStroke.Color = Color3.fromRGB(125, 80, 54)
		end
	else
		buyButton.Text = "PURCHASE"
		buyButton.BackgroundColor3 = Color3.fromRGB(90, 255, 105)
		buyButton.borderStroke.Color = Color3.fromRGB(44, 125, 51)
	end
end

-- Loops to update the infoFrame information
for i,v in pairs(meleeFrame:GetDescendants()) do
	if v:IsA("TextButton") and v.Name == "itemButton" then
		local item = v.Parent

		v.MouseButton1Click:Connect(function()
			infoFrame:FindFirstChild("itemViewFrame").Visible = true
			infoFrame:FindFirstChild("buyButton").Visible = true

			clearViewFrame()

			local newViewFrame = item.ViewportFrame:Clone()
			newViewFrame.Parent = infoFrame:FindFirstChild("itemViewFrame")
			newViewFrame.Position = UDim2.new(0, 0, 0, 0)
			newViewFrame.Size = UDim2.new(1, 0, 1, 0)

			infoFrame.miscInfo:FindFirstChild("1_name").Text = "Name: ".. item:FindFirstChild("_Name").Value
			infoFrame.miscInfo:FindFirstChild("2_description").Text = "Description: ".. item:FindFirstChild("_Description").Value
			infoFrame.miscInfo:FindFirstChild("3_price").Text = "C$".. item:FindFirstChild("_Price").Value

			nameVal.Value = item:FindFirstChild("_Name").Value
			priceVal.Value = item:FindFirstChild("_Price").Value
		end)
	end
end

for i,v in pairs(miscFrame:GetDescendants()) do
	if v:IsA("TextButton") and v.Name == "itemButton" then
		local item = v.Parent

		v.MouseButton1Click:Connect(function()
			infoFrame:FindFirstChild("itemViewFrame").Visible = true
			infoFrame:FindFirstChild("buyButton").Visible = true

			clearViewFrame()

			local newViewFrame = item.ViewportFrame:Clone()
			newViewFrame.Parent = infoFrame:FindFirstChild("itemViewFrame")
			newViewFrame.Position = UDim2.new(0, 0, 0, 0)
			newViewFrame.Size = UDim2.new(1, 0, 1, 0)

			infoFrame.miscInfo:FindFirstChild("1_name").Text = "Name: ".. item:FindFirstChild("_Name").Value
			infoFrame.miscInfo:FindFirstChild("2_description").Text = "Description: ".. item:FindFirstChild("_Description").Value
			infoFrame.miscInfo:FindFirstChild("3_price").Text = "C$".. item:FindFirstChild("_Price").Value

			nameVal.Value = item:FindFirstChild("_Name").Value
			priceVal.Value = item:FindFirstChild("_Price").Value
		end)
	end
end

-- Main script
nameVal.Changed:Connect(function(item)
	warn(item)
	updateBuyButton()
	
	buyButton.MouseButton1Click:Connect(function(something)
		warn(something)
		if buyButton.Text == "PURCHASE" then -- Purchasing an item
			local price = priceVal.Value
			
			if player.leaderstats.Coins.Value >= price then -- Update the player's leaderstats
				warn("Purchased item: ".. nameVal.Value)
				buyButton.Text = "EQUIP"
				
				local itemName = nameVal.Value
				REFolder.buyItemRE:FireServer(itemName, price)
				buyButton.BackgroundColor3 = Color3.fromRGB(100, 255, 145)
				buyButton.borderStroke.Color = Color3.fromRGB(49, 125, 69)
				
				for i, v in pairs(frames:GetDescendants()) do
					if v:IsA("TextLabel") and v.Name == "itemName" and v.Text == nameVal.Value then
						local item = v.Parent
						local frame = item.Parent.Parent
						
						updateItems(frame)
					end
				end
			else -- Player is short on coins to buy an item
				buyButton.Text = "NOT ENOUGH"
				buyButton.BackgroundColor3 = Color3.fromRGB(255, 75, 75)
				buyButton.borderStroke.Color = Color3.fromRGB(125, 37, 37)
				warn("Can't purchase item: ".. nameVal.Value)
				task.wait(1)
				buyButton.Text = "PURCHASE"
				buyButton.BackgroundColor3 = Color3.fromRGB(90, 255, 105)
				buyButton.borderStroke.Color = Color3.fromRGB(44, 125, 51)
			end
		elseif buyButton.Text == "EQUIP" then -- Equipping an item
			warn("Equipped item: ".. nameVal.Value)
			local itemName = nameVal.Value
			REFolder.equipItemRE:FireServer(itemName)
			buyButton.Text = "UNEQUIP"
			buyButton.BackgroundColor3 = Color3.fromRGB(255, 165, 110)
			buyButton.borderStroke.Color = Color3.fromRGB(125, 80, 54)
			
		elseif buyButton.Text == "UNEQUIP" then -- Unequipping an item
			warn("Unequipped item: ".. nameVal.Value)
			buyButton.Text = "EQUIP"
			buyButton.BackgroundColor3 = Color3.fromRGB(100, 255, 145)
			buyButton.borderStroke.Color = Color3.fromRGB(49, 125, 69)

			if player.Backpack:FindFirstChild(nameVal.Value) then
				player.Backpack:FindFirstChild(nameVal.Value):Destroy()
			elseif player.Character:FindFirstChild(nameVal.Value) then
				player.Character:FindFirstChild(nameVal.Value):Destroy()
			end
		end
	end)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

After looking over your code, I can see how the buyButton connection occurs multiple times for the reason you stated in your post.

You may want to consider moving your buybutton.MouseButton1Click:Connect script out from under the nameVal.Changed:Connect thing. That means that the piece of code won’t ever occur more than one time (unless you duplicate the script or something).

Doing this may require a little change in the logic under your buyButton connection to stop bugs, but I think it would be a much better way to do it.

Please tell me if I didn’t understand how your script functions, and if so, how it functions that requires the buybutton connection to be housed under the nameVal connection.

1 Like

whats placing the cola inside backpack

A remote event but I don’t think the code to the remote event could be the issue. Here’s the code just in case

local RS = game:GetService("ReplicatedStorage")
local REFolder = RS:WaitForChild("RemoteEvents").shopSystem

local SS = game:GetService("ServerStorage")

REFolder.equipItemRE.OnServerEvent:Connect(function(player, itemName)
	local item = SS.StoreItems:FindFirstChild(itemName)
	item:Clone().Parent = player.Backpack
end)
1 Like

I was thinking about doing it but then I realized, how would the text change to “EQUIP” or “UNEQUIP” if it won’t be able to update with the nameVal.Value.

Nevermind, you were right. I didn’t think about the updateBuyButton() function in the nameVal.Changed event despite the fact I placed the updateBuyButton() function. I wasted so many hours solving this just to move the buyButton.MouseButton1Click:Connect event out of the nameVal.Changed event. Thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.