Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Connecting and disconnecting through a table?

So I'm creating a lil system to allow Mouse and Keyboard, as well as gamepad to work in a GUI, by only creating necessary connections when entering a menu and having them removed on leaving the menu. Sounds good, but lets hold that thought, because leaving a menu and returning creates an additional connection! WOAH! So now every button press happens twice, which means if I wanted to change something from true to false, it goes, true>false>true again. Aghh! D:< Here is the basic structure:

local ScreenConnections = {}

function ChangeScreen(NewScreen)
	if CurrentScreen ~= nil then
		CurrentScreen.Visible = false
		if CurrentSelection~=nil then
		CurrentSelection.BackgroundTransparency = 1
		CurrentSelection.BorderSizePixel = 1
		PastScreen = CurrentScreen
		end
	end
	print(#ScreenConnections)
	if #ScreenConnections > 0 then
		for _,i in pairs(ScreenConnections) do
			i:disconnect()
		end
	end
	CurrentScreen = NewScreen
	CurrentScreen.Visible = true
	CurrentSelection = nil
	for _,i in pairs(CurrentScreen:GetChildren()) do
		if i.ClassName == "TextButton" then
			ScreenConnections[i.Name.."MouseEnter"] = i.MouseEnter:connect(function()SelectButton(i)end)
			ScreenConnections[i.Name.."MouseLeave"] = i.MouseLeave:connect(function()SelectButton()end)
			ScreenConnections[i.Name.."MouseClick"] = i.MouseButton1Click:connect(function()ActivateButton(i)end)
		end
		if CurrentScreen.Name == "Options" and i.ClassName == "Frame" then
			local InSub = false
			local butt = i:FindFirstChild("Header")
			local info = i:FindFirstChild("Info")
			local Enable = i:FindFirstChild("Enable")
			local Scroll = i:FindFirstChild("Scroll")
			local SubMenu = i:FindFirstChild("SubMenu")
			ScreenConnections[i.Name.."MouseEnter"] = i.MouseEnter:connect(function()info.Visible = true end)
			ScreenConnections[i.Name.."MouseLeave"] = i.MouseLeave:connect(function()info.Visible = false end)
			--butt.MouseEnter:connect(function()SelectButton(butt) end)
			--butt.MouseLeave:connect(function()SelectButton()end)
			if Enable then
				ScreenConnections[i.Name..Enable.Name.."MouseEnter"] = Enable.MouseEnter:connect(function()SelectButton(Enable)end)
				ScreenConnections[i.Name..Enable.Name.."MouseLeave"] = Enable.MouseLeave:connect(function()Enable.BorderSizePixel = 0 end)
				ScreenConnections[i.Name..Enable.Name.."MouseClick"] = Enable.InputBegan:connect(function(InObj) if InObj.UserInputType == Enum.UserInputType.MouseButton1 then
											ActivateButton(Enable)
											end 
										end)
				Enable.BorderSizePixel = 0
			end
			if info then
				info.Visible = false
			end
			if Scroll then
				ScreenConnections[i.Name..Scroll.Name.."MouseDown"] = Scroll.InputBegan:connect(function(a)
					if a.UserInputType == Enum.UserInputType.MouseButton1 and a.UserInputState == Enum.UserInputState.Begin then
						MoveConn = Scroll.MouseMoved:connect(function(X) print("FOR NARNIA") UpdatePos(X,Scroll) wait(.2) print("") end)
						end
				end)

				ScreenConnections[i.Name..Scroll.Name.."MouseUp"] = Scroll.InputEnded:connect(function()
					local Min = Scroll:FindFirstChild("Min")
					if Min then
						if Scroll.Bar.Position.X.Offset < Min.AbsoluteSize.X then
							Scroll.Bar.Position = UDim2.new(-1,Min.AbsoluteSize.X,0,0)
						end
					end
					if MoveConn ~= nil then
						MoveConn:disconnect()
						MoveConn = nil
					end
				end)
			end
		end
	end
end

ChangeScreen(Menu.Main)

Everything is properly connected, all functions fire, and it runs normally, but the table doesn't seem to be updated. This majorly sucks. Any idea what I've done wrong?

2 answers

✓ Accepted answer

Dictionaries

Dictionaries don't have a length. The length is decided by Lua by finding the largest non-nil integer key. In this case, you don't need the length check - If it doesn't have anything, pairs simply won't iterate.

local ScreenConnections = {}

function ChangeScreen()
        for _,i in pairs(ScreenConnections) do
		print('disconnecting', _)
            i:disconnect()
        end
ScreenConnections[i.Name.."MouseEnter"] = i.MouseEnter:connect(function()SelectButton(i)end)
            ScreenConnections[i.Name.."MouseLeave"] = i.MouseLeave:connect(function()SelectButton()end)
            ScreenConnections[i.Name.."MouseClick"] = i.MouseButton1Click:connect(function()ActivateButton(i)end)
end

1 comment

0

I didn't know that, I've been doing # checks for all my tables since the dawn of time. Thanks!

The method disconnect is obsolete RBXScriptSignal

From the wiki this may help:-

A connection is automatically disconnected if:

• The event listener generates an error before the thread yields.

• The script itself is removed or reparented.

• The object the Event relates to is destroyed with the Destroy method.

2 comments

0

Its actually used in the Control and Camera scripts frequently. I have a working example, but instead of a table it creates a global value.

0

It's not obsolete when regarding connections, which are the objects which the :disconnect member is coming from.

This discussion is preserved as it was. New answers, comments, and votes are closed.