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

script runs even after player no longer meets requirements?

Hello, I am unknown why this program doesn't work like it is meant to. Basically it checks if a player is in the seat, then allows them to 'press e' (For debugging atm) and only lets them do that if they remain in the seat. However I noticed that it allows it to continue even after the player left the seat.

Anyone got a solution? Much thanks!

local UserInputService = game:GetService("UserInputService")
local Car = game.Workspace.Car
local CarBase = Car.CarBase
local CarMoving = Car.CarMoving
local CarSeat = Car.Seat
local Player = game.Players.LocalPlayer
CarSeat:GetPropertyChangedSignal("Occupant"):Connect(function (humanoid) -- Checking that there is a person in the driver seat
	if CarSeat.Occupant then -- Occupant Exists
		if CarSeat.Occupant.Parent.Name == script.Parent.Parent.Name then -- Person in the seat is the local player
       	 game:GetService("UserInputService").InputBegan:connect(function(input) -- Input Began checks if the player presses anything and fires whenever the player does so.
				if input.KeyCode == Enum.KeyCode.E then -- This detects for a "e" press.
					print("E is")
				end
				end)
			end
	
		game:GetService("UserInputService").InputEnded:connect(function(input) -- Fires when input ended
			if input.KeyCode == Enum.KeyCode.E then
				print("E is no more")
			end
   			
		end)
	else
		print(CarSeat.occupant)
	end
end)
	
	
		
		

1 answer

✓ Accepted answer

This happens because connections don't last only as long as a condition is valid. Connections must be disconnected through their :Disconnect() method. This stops the connection from running again. This can be implemented in your script as well:

local UserInputService = game:GetService("UserInputService")
local Car = game.Workspace.Car
local CarBase = Car.CarBase
local CarMoving = Car.CarMoving
local CarSeat = Car.Seat
local Player = game.Players.LocalPlayer
local Connection -- We don't want the scope of the if statements to trouble us
local StopConnection

CarSeat:GetPropertyChangedSignal("Occupant"):Connect(function(humanoid)
	if CarSeat.Occupant then -- Occupant Exists
		if CarSeat.Occupant.Parent.Name == Player.Name then -- Verify player
			Connection = game:GetService("UserInputService").InputBegan:Connect(function(input) -- New connectoin
				if input.KeyCode == Enum.KeyCode.E then
					print("E is")
				end
			end)
		end
		StopConnection = game:GetService("UserInputService").InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.E then
				print("E is no more")
				Connection:Disconnect() -- Disconnect connection
				StopConnection:Disconnect() -- Also disconnect this so it doesn't pile up
			end
		end)
	else
		print(CarSeat.occupant)
		Connection:Disconnect() -- In case the player jumps out with E pressed, not sure if this is applicable in your case
	end
end)

P.S. Use Connect, not connect. connect() is deprecated.

1 comment

0

I had to move the stop connections to when the user leaves the seat, but otherwise it works! Thanks!

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