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.
local toggle = 0 function lightsOn() if toggle == 0 then toggle = 1 lights.Material = "Neon" lights.Spotlights.Enabled = true --Yes i know lights.Spotlights wont work, its just metaphorical elseif toggle == 1 then toggle = 0 Lights.Material = "Plastic" lights.Spotlights.Enabled = false end 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
local UIS = game:GetService("UserInputService") local debounce = true local player = game:GetService("Players").LocalPlayer local character = player.Character or player.CharacterAdded:Wait() UIS.InputBegan:Connect(function(input) if input.UserInputType == Enum.UserInputType.Keyboard then --make sure the press is from the keyboard local val = input.KeyCode.Value if val == 108 then --108 is L if character:FindFirstChild("Humanoid").Seated == true then if script.Parent.Occupant.Parent.Name == player.Name then--Assuming the parent of the script is the vehicle seat if debounce then debounce = false --do thing debounce = true end end end end end end
Sorry if this didn't work for you