Connection not Disconnecting

I made a connection which is how it is down below;

local UIS = game:GetService("UserInputService")
local KeyPress

local function TheFunction(input, isTyping)
   if isTyping then return end
   if input.KeyCode == Enum.KeyCode.Key then
      -- Script here:
      KeyPress:Disconnect()
      KeyPress = nil
   end
end

KeyPress = UIS.InputBegan:Connect(TheFunction)

When I do this, the connection does not disconnect and it still goes on.

I need to fix this so the code can be executed properly.

Are you sure UIS.InputBegan:Connect(TheFunction) isn’t happening more than once?

Hold on, this is the script;

DarkJosieSection:NewToggle(
					"Telekinesis",
					"[Key: E] Press the key to cast the spell!",
					function(state)
						local UIS = game:GetService("UserInputService")
						local KeyPress, KeyPressEnd
						if state then
							local AbilityName = "Telekinesis"
							warn('Successfully toggled on: '..AbilityName..'!')
							local function StartingAbility(input, isTyping)
								if not game.Players.LocalPlayer.Character then return end
								if not game.Players.LocalPlayer.Character:FindFirstChild("Stats") then return end
								if isTyping then return end
								if game.Players.LocalPlayer.Character.Stats.CharacterName.Value == "Dark Josie" then
									if input.KeyCode == Enum.KeyCode.E then
										for i, v in pairs(game:GetService("Players"):GetChildren()) do
											if not v:FindFirstChild("PlayerGui") then
												if game.Players.LocalPlayer.Character.Stats.CharacterName.Value == "Dark Josie" then
													local Mouse = game.Players.LocalPlayer:GetMouse()
													local target = nil
													local HoldClick, HoldClickEnd, pr, cr

													HoldClick = Mouse.Button1Down:Connect(function()
														if Mouse.Target then
															if Mouse.Target.Parent:FindFirstChild("Humanoid") then
																target = Mouse.Target.Parent
															elseif Mouse.Target.Parent.Parent:FindFirstChild("Humanoid") then
																target = Mouse.Target.Parent.Parent
															end
														end

														local args = {
															[1] = {
																["Target"] = workspace:WaitForChild("Players"):WaitForChild(target.Name),
																["mouse"] = Mouse.Hit,
																["Action"] = "Ascendo"
															},
															[2] = true,
															[3] = "start"
														}
														game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("SystemEvents"):WaitForChild("KeyBinds"):FireServer(unpack(args))
													end)
													HoldClickEnd = Mouse.Button1Up:Connect(function()
														local Mouse = game.Players.LocalPlayer:GetMouse()

														local function getNil(name,class) for _,v in next, getnilinstances() do if v.ClassName==class and v.Name==name then return v;end end end

														local args = {
															[1] = workspace:WaitForChild("Players"):WaitForChild(game.Players.LocalPlayer.Name),
															[2] = getNil(target.Name, "Model"),
															[3] = "Ascendo",
															[4] = Mouse.Hit,
															[5] = true,
															[6] = false
														}
														game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("SystemEvents"):WaitForChild("KeyBindUp"):FireServer(unpack(args))
														HoldClick:Disconenct()
														HoldClick = nil
														HoldClickEnd:Disconnect()
														HoldClickEnd = nil
													end)
													pr = game.Players.PlayerRemoving:Connect(function(plr)
														if plr == game.Players.LocalPlayer then
															HoldClick:Disconnect()
															HoldClickEnd:Disconnect()
															pr:Disconnect()
															pr = nil
															cr:Disconnect()
															cr = nil
														end
													end)
													cr = game.Players.LocalPlayer.CharacterRemoving:Connect(function()
														HoldClick:Disconnect()
														HoldClickEnd:Disconnect()
														pr:Disconnect()
														pr = nil
														cr:Disconnect()
														cr = nil
													end)
												end
											end
										end
									end
								end
							end
							KeyPress = UIS.InputBegan:Connect(StartingAbility)
						else
							local AbilityName = "Telekinesis"
							warn('Successfully toggled off: '..AbilityName..'!')
							KeyPress = nil
							KeyPressEnd = nil
						end
					end
				)

After MouseButton1Up is done it’s supposed to disconnect both functions until the E key is pressed again and the mouse button is held down on the victim; etc.

But it repeatedly does it and never disconnects.

There’s a difference between storing the value of something, and storing the reference of something. For example, numbers are stored in the variable itself. However, when you store a table in a variable, it is not stored in the variable, but instead the variable stores the address of the table in memory, basically telling the computer where the table is.

The same goes for RBXScriptConnection objects. In the code, you’re setting the KeyPress variable to nil, which does not destroy the connection. It instead, destroys the reference to that connection. To actually disconnect the connection, replace KeyPress = nil to KeyPress:Disconnect() instead.

1 Like

Well you see, I did that and it didn’t work. But let mee try again.

Try using Once() instead of Connect().

Once is a connection that disconnects itself after it fires a single time.
You could try doing this instead.

local UIS = game:GetService("UserInputService")
--local KeyPress

local function TheFunction(input, isTyping)
   if isTyping then return end
   if input.KeyCode == Enum.KeyCode.Key then
      -- Script here:
   else
      UIS.InputBegan:Once(TheFunction)
   end
end

UIS.InputBegan:Once(TheFunction)

This event connection should disconnect each time it fires but if the condition isn’t met it should re-create the connection.

Also in the future, to prevent errors you should do

if connection.Connected then
  connection:Disconnect()
end

Connections can throw and error if you try to disconnect it while it’s already disconnected.

Lemme know if this worked.

3 Likes

I will go ahead and try all of this, thank you for your help.

1 Like

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