Hello, I am struggling with this, I am trying to make a game that will involve vehicles that can perform functions when the player in the driver seat presses buttons. For example, if the driver was to press L, it would run a function like this to turn the lights on.
01 | local toggle = 0 |
02 | function lightsOn() |
03 | if toggle = = 0 then |
04 | toggle = 1 |
05 | lights.Material = "Neon" |
06 | lights.Spotlights.Enabled = true --Yes i know lights.Spotlights wont work, its just metaphorical |
07 | elseif toggle = = 1 then |
08 | toggle = 0 |
09 | Lights.Material = "Plastic" |
10 | lights.Spotlights.Enabled = false |
11 | end |
12 | end |
But I cant find any way of detecting the player pressing a button only if they are on the seat. I always hit a brick wall, I have tried accessing the player through the seat weld then discovering you can only use Player.GetMouse() in local scripts, I have also tried reverse engineering plane seats because they use P for full speed only to remember they use a tool. I really don't want the player to have to use a tool while in the seat because they will be using other tools while driving. all i'm looking for is a way to be able to make any button on the keyboard do something as if you were clicking a part using the ClickDetector. I would like multiple buttons per vehicle and also there will be many vehicles so I don't want any of the vehicles interfering with each other. If anyone can help I will be very grateful.
We will be using UserInputServiced and Humanoid.Seated property
01 | local UIS = game:GetService( "UserInputService" ) |
02 | local debounce = true |
03 | local player = game:GetService( "Players" ).LocalPlayer |
04 | local character = player.Character or player.CharacterAdded:Wait() |
05 | UIS.InputBegan:Connect( function (input) |
06 | if input.UserInputType = = Enum.UserInputType.Keyboard then --make sure the press is from the keyboard |
07 | local val = input.KeyCode.Value |
08 | if val = = 108 then --108 is L |
09 | if character:FindFirstChild( "Humanoid" ).Seated = = true then |
10 | if script.Parent.Occupant.Parent.Name = = player.Name then --Assuming the parent of the script is the vehicle seat |
11 | if debounce then |
12 | debounce = false |
13 | --do thing |
14 | debounce = true |
15 | end |
16 | end |
17 | end |
18 | end |
19 | end |
20 | end |
Sorry if this didn't work for you