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

InputBegan won't fire no matter which key I press?

Asked by 4 years ago

Here's my extremely short code that theoretically should run when a key is pressed:

game:GetService("UserInputService").InputBegan:Connect(function(key)
    script.Parent.ServerKeyboard:FireServer(key)
end)

However it doesn't fire when I press a key. I'm trying to use this to detect inputs from the player sitting on a vehicle.

1 answer

Log in to vote
4
Answered by 4 years ago
Edited 4 years ago

As you said, you want to fire the event on Keyboard key press (I assume), but you have an incomplete code.

Instead of:

game:GetService("UserInputService").InputBegan:Connect(function(key)

-- Make it
game:GetService("UserInputService").InputBegan:Connect(function(input)

And the lines I am gonna add next will make sure that the key from keyboard is pressed and not a Mouse button or something.

if input.UserInputType == Enum.UserInputType.Keyboard then

If you want to know the key that is pressed, you can create a local variable for that:

local PressedKey = input.KeyCode

Extra Info: If you wanna fire the event on specific key press, you can do:

if PressedKey = Enum.KeyCode.E then -- If 'e' key is pressed

This is how it will look in your code (Commented the extra info script):

game:GetService("UserInputService").InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInput.Type.Keyboard then
        local PressedKey = input.KeyCode
    --  if PressedKey == Enum.KeyCode.E then
            script.Parent.ServerKeyboard:FireServer(PressedKey)
    --  end
    end
end)

Lemme know if it helps!

Ad

Answer this question