I have an issue with detecting pressed key in roblox studio, but it didn’t work.WHY!
UserInputService.InputBegan:Connect(function(input, inputObject)
if inputObject.InputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
print("Pressed", input.KeyCode)
end
end
end)
The 2nd return of InputBegan is boolean that determines if the game recognized this input, such as if you press a key on your keyboard when you’re focused on a textbox. If you want to check if the Input was from a keyboard, just do input.UserInputType instead of inputObject.InputType, and rename it so you don’t get confused
Looks like it should work since there’s nothing wrong with it, maybe remove the first if statement since you’re only checking if a certain key is pressed?
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
print("Pressed", input.KeyCode)
end
end)
the second parameter is for the game processed event. Like when a player is chatting and pressing a specific key.
Like to enter a car, Press “E”
and imagine the player is near the car and chatting, and pressed the “E” key. the player goes in the car. but u dont want that . so, to prevent situations like this “The UserInputService” has the Game Processed Event to check if the player is typing something in the chat
UserInputService.InputBegan:Connect(function(input, GPE)
if GPE then return end
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
print("Pressed", input.KeyCode)
end
end
end)
try this and put in localscript it in starterplayerscripts