What do you want to achieve?
I just want this function to disconnect once a player has left the driver seat.
What is the issue?
It stays connected.
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.
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)
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)
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.
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.
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
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.
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.)
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.