[SOLVED] :Disconnect() Not Working

  1. What do you want to achieve?
    I just want this function to disconnect once a player has left the driver seat.

  2. What is the issue?
    It stays connected.

  3. What solutions have you tried so far?
    Ive messed around with it a bit, even looked around on the forum but I cannot find a decent solution.


--This script is a Local Script in a GUI in PlayerGui
local Vehicle = script.Parent.Parent.Car.Value --This is an object value
local A

A = game:GetService("UserInputService").InputBegan:Connect(function(INPT,GPE)
	if not GPE then
		if INPT.KeyCode ~= Enum.KeyCode.Unknown then
			if INPT.KeyCode == Enum.KeyCode.H then
				print("Working")
			end
		end
	end
end)

Vehicle:FindFirstChildWhichIsA("VehicleSeat").ChildRemoved:connect(function(child)
	if child.Name=="SeatWeld" then
		print("Removing") --Yes when I leave the seat this does print in the output.
		A:Disconnect() --Supposed to disconnect the function here but it never does.
	end
end)

To further clarify:
This is in a GUI that is automatically put into the player’s GUI once the player has sat down in the seat. The GUI is also deleted while the player is getting out of the seat. This is on an A-Chassis car.

Please I’ve been struggling with this for like the past hour.

5 Likes

you’re binding the input function once then disconnecting the input connection

you also don’t need to use a ChildRemoved check for a seat weld

so do something like this

--store our local connection variable
local Connection
local LP = game.Players.LocalPlayer

Vehicle.VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
  local Occupant = Vehicle.VehicleSeat.Occupant
  --clear the connection whenever the occupant is changed
  if Connection then Connection:Disconnect() Connection = nil end

  --check if the occupant is our local player
  if LP.Character and Occupant == LP.Character.Humanoid then
    --set the connection to be cleaned up on the next call
    Connection = game:GetService("UserInputService").InputBegan:Connect(function()
      --code here
    end)
  end
end)
4 Likes

This trigger won’t work in a local script. I need some other way around this.

3 Likes

The occupant property is triggering correctly from a LocalScript on a Baseplate. Are you sure this is the issue?

2 Likes

You can check if the connection is a RBXScriptConnection by doing something this:

if typeof(Connection) == "RBXScriptConnection" and Connection.Connected then 
   Connection:Disconnect()
   Connection = nil
end

otherwise than this, i dont really know what else to do.

3 Likes

Rewrote the script only changing the problematic code:

local Vehicle = script.Parent.Parent.Car.Value --This is an object value
local A

local function onInputBegan(INPT,GPE)
	if not GPE then
		if INPT.KeyCode ~= Enum.KeyCode.Unknown then
			if INPT.KeyCode == Enum.KeyCode.H then
				print("Working")
			end
		end
	end
end

A = game:GetService("UserInputService").InputBegan:Connect(onInputBegan)

Vehicle:FindFirstChildWhichIsA("VehicleSeat").ChildRemoved:connect(function(child)
	if child.Name=="SeatWeld" then
		print("Removing") --Yes when I leave the seat this does print in the output.
		A:Disconnect() --Supposed to disconnect the function here but it never does.
	end
end)
3 Likes

all of that code is problematic though, you’re never actually binding the InputBegan function when the player gets in the seat. you’re just binding it once and then having it get disconnected.

yes it will I tested it


image

3 Likes

I can’t reproduce this issue with your code. Is there more than one car? i.e. when you hit H does “Working” print just once per keypress? The input bind happens as soon as the script executes, so every car with this script should be responding to pressing H, until you’ve gotten in and out of each one at least once.

3 Likes

Its probably not changing because the GUI is spawned into the player after they get into the seat and deleted while they get out.

2 Likes

thats an extremely important detail lol. However binds get automatically cleared when a script is destroyed. So if your script is inside your gui and it gets destroyed when the player leaves then try not trying to disconnect it. If your script is not getting destroyed then throw it in the gui

2 Likes

It still will not disconnect for me, unfortunately. Im losing my mind trying to figure out what is not working.

2 Likes

This only applies to certain connections. Some connections are not cleared automatically.

2 Likes

I just tried adding a disconnect for when the script is being destroyed too and it still will not disconnect.

2 Likes

InputBegan connections are though

2 Likes

This is what I don’t understand, is that it is showing that it is removing but when I check to see if they disconnected, it still shows that they are there.

2 Likes

This is normal; there will still be a “connection” stored but it’s inactive. What really matters is whether or not the connections still work.

2 Likes

Yes, the connections still work. If I get back in the driveseat it will start running those functions another additional time for each time I get back into the driveseat.

So if I get into the drive seat 3x it will run the InputBegan function 3 times even though there is one script in existence.

(Keep in mind there is a main server script that clones the GUI and deletes the GUI each time the player gets in or out of the seat.)

2 Likes

Could you add something like print(script.Parent) to one of the connections to confirm the scripts are actually gone? I’m unable to reproduce this problem. All my connections are disconnecting even when the script is cloned from the server.

2 Likes

I love 30 text limit thingy…

1 Like

Printing the connections doesn’t confirm they’re inactive. Press H or whatever causes the connections to print a message.

2 Likes