Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Detect player pressing buttons on a seat?

Asked by 3 years ago

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.

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

0
I appreciate your help but I can't seem to get the script to work. where it said --do thing I put print("Pressed") for testing. When I clicked run, i got an error line 20:"attempt to index nil with 'InputBegan'", After changing the UserInputService on line 20 to UIS the error went away but it still doesn't work BlockBob12 3 — 3y
0
Ah, sorry. I confused the variable names. I'll change my answer to solve the errors. What are the errors after changing the variable name to UIS? nekosiwifi 398 — 3y
0
Keep in mind UserInputService only worked on LocalScripts nekosiwifi 398 — 3y
0
After changing the the variable to UIS, I got nothing at all but it wasnt working, I spotted a missing set of brackets on line 20 "UIS.InputBegan:connect(lightsOn())" those two after lightsOn were missing, after changing that I now have an error saying "Workspace.VehicleSeat.Script:6: attempt to index nil with 'UserInputType'" I also tried it in a localscript and no luck BlockBob12 3 — 3y
View all comments (5 more)
0
What happened when you tried it in a localscript? You don't need to add a set of brackets if you're making connecting an event to a seperate function. nekosiwifi 398 — 3y
0
Whe I tried it in a localscript, it did the same thing as before. Adding brackets also made the function run, without them it just stopped at UIS.InputBegan:connect(lightsOn()). I have been experimenting with your new script and havent got any results from it, just a missing bracket to close line 5 that I put after the last end on line 20, still doesnt work though BlockBob12 3 — 3y
0
Also, apparently LocalScripts only run if they are inside the player like in backpack, playersripts or playergui etc. Mabye theres a way to do it by checking what player sat on the seat and then cloning a local script into backpack or (PlayerScripts isnt accessable by the server) and removing it when the player guts up? BlockBob12 3 — 3y
0
Alright, looks like I'm stuck here then. Looks like you're on your own then. Sorry for the inconvinience. You don't have to clone it though. nekosiwifi 398 — 3y
0
Ok no worries, thanks for trying. I think the main problem at the moment is that pressing a key isnt triggering UIS.InputBegan:connect() BlockBob12 3 — 3y
Ad

Answer this question