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!
01 | local UserInputService = game:GetService( "UserInputService" ) |
02 | local Car = game.Workspace.Car |
03 | local CarBase = Car.CarBase |
04 | local CarMoving = Car.CarMoving |
05 | local CarSeat = Car.Seat |
06 | local Player = game.Players.LocalPlayer |
07 | CarSeat:GetPropertyChangedSignal( "Occupant" ):Connect( function (humanoid) -- Checking that there is a person in the driver seat |
08 | if CarSeat.Occupant then -- Occupant Exists |
09 | if CarSeat.Occupant.Parent.Name = = script.Parent.Parent.Name then -- Person in the seat is the local player |
10 | game:GetService( "UserInputService" ).InputBegan:connect( function (input) -- Input Began checks if the player presses anything and fires whenever the player does so. |
11 | if input.KeyCode = = Enum.KeyCode.E then -- This detects for a "e" press. |
12 | print ( "E is" ) |
13 | end |
14 | end ) |
15 | end |
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:
01 | local UserInputService = game:GetService( "UserInputService" ) |
02 | local Car = game.Workspace.Car |
03 | local CarBase = Car.CarBase |
04 | local CarMoving = Car.CarMoving |
05 | local CarSeat = Car.Seat |
06 | local Player = game.Players.LocalPlayer |
07 | local Connection -- We don't want the scope of the if statements to trouble us |
08 | local StopConnection |
09 |
10 | CarSeat:GetPropertyChangedSignal( "Occupant" ):Connect( function (humanoid) |
11 | if CarSeat.Occupant then -- Occupant Exists |
12 | if CarSeat.Occupant.Parent.Name = = Player.Name then -- Verify player |
13 | Connection = game:GetService( "UserInputService" ).InputBegan:Connect( function (input) -- New connectoin |
14 | if input.KeyCode = = Enum.KeyCode.E then |
15 | print ( "E is" ) |
P.S. Use Connect, not connect. connect() is deprecated.