How would I structure this better to prevent .Activated events from stacking?

I’m having a problem where .Activated events would stack. It works fine at the first click and only fires the remote once, if you click the button the second time, it fires the remote twice, and it repeatedly stacks up each click after that. How would I structure this to prevent that?

local List = Main.List

for _, v in ipairs( List:GetDescendants() ) do
	if v:IsA('TextButton') then
		v.Activated:Connect(function()
			Obtain.Visible = true
			Obtain.Title.Text = BadgeInfo.Spiker[v.Parent.Name].Title
			Obtain.Description.Text = BadgeInfo.Spiker[v.Parent.Name].Description
			
			for _, k in ipairs( Obtain:GetChildren() ) do
				if k:IsA('TextButton') then
					k.Activated:Connect(function()
						local Badge = BadgeRemote:InvokeServer(v.Parent.Name, k.Name)
						Obtain.Visible = false

						for _, v in ipairs(Misc.TextMain:GetChildren()) do
							if v.Name == 'UpgradePoints' then
								v.Text = Badge[2]
							end
						end

						if Badge and #Badge[1] >= 1 and Badge[1] ~= 0 then
							v.Parent.Icon.ImageColor3 = Color3.fromRGB(255, 255, 255)
							v.Parent.Icon.Image = BadgeLevels[Badge[1][v.Parent.Name]]
						else
							v.Parent.Icon.Image = BadgeLevels[4]
							v.Parent.Icon.ImageColor3 = Color3.fromRGB(50, 50, 50)
						end
					end)
				end
			end
		end)
	end
end
1 Like

Maybe try MouseButton1Click or MouseClick rather than .Activated?

I’m currently not on my PC, so I can’t provide a example script that I use that’s extremely similar to this scenario, but to put it simply, try replacing .Activated with a click action

(I say this in assumption that you’re trying to make the function begin when the textbutton is activated)

.activated is better since it works on xbox, pc, and mobile. Mousebutton 1 some times may not fire on devices outside of pc

You should add a table to store the connections and disconnect them when activated again.

local List = Main.List

for _, v in ipairs( List:GetDescendants() ) do
	if v:IsA('TextButton') then
		local conns = {}
		v.Activated:Connect(function()
			for i,v in pairs(conns) do
				v:Disconnect()
			end
			Obtain.Visible = true
			Obtain.Title.Text = BadgeInfo.Spiker[v.Parent.Name].Title
			Obtain.Description.Text = BadgeInfo.Spiker[v.Parent.Name].Description
			
			for _, k in ipairs( Obtain:GetChildren() ) do
				if k:IsA('TextButton') then
					conns[#conns+1] = k.Activated:Connect(function()
						local Badge = BadgeRemote:InvokeServer(v.Parent.Name, k.Name)
						Obtain.Visible = false

						for _, v in ipairs(Misc.TextMain:GetChildren()) do
							if v.Name == 'UpgradePoints' then
								v.Text = Badge[2]
							end
						end

						if Badge and #Badge[1] >= 1 and Badge[1] ~= 0 then
							v.Parent.Icon.ImageColor3 = Color3.fromRGB(255, 255, 255)
							v.Parent.Icon.Image = BadgeLevels[Badge[1][v.Parent.Name]]
						else
							v.Parent.Icon.Image = BadgeLevels[4]
							v.Parent.Icon.ImageColor3 = Color3.fromRGB(50, 50, 50)
						end
					end)
				end
			end
		end)
	end
end
1 Like

True, I’m unsure if they plan to make the game playable across platforms