UserInputService issue

Hello, the lost people,

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

1 Like

still didn’t work. Do you have other solution?

Can you show me your attempted solution

image

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?

Still doesn’t work.The output is empty
image

Are you certain you didn’t just filter out client prints? Remove all the if statements and just print out input.KeyCode and see if that does print.

And to also ask, are you making sure you’re using this in a Localscript in a location where it can run?

try out this script, this works for me

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print("Pressed", input.KeyCode)
	end
end)

(make sure it is a local script)

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

Doesn’t work. It didn’t even print anything

where is the script located? Can I see the path?

image
Workspace

Localscript don’t work in workspace, put it somewhere like StarterPlayer → StarterPlayerScripts

These are all the places localscripts work in since I know someone is going to comment if I say “Descendant of something related to the player” again

2 Likes
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

Tysm for helping me fix this. I start to learn how to script since I was 9. So I’m still new at this

2 Likes