Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to prevent key input while player is typing in chat?

So, I wrote a gun script, but when the player types the letters E or R in the chat, it will toggle the safety or reload the gun, accordingly.

I don't want this to happen. I've seen plenty of games that don't do this, so I know there's a way.

Here's the script. I've shortened it down considerably, to the parts that are relevant to this problem.

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
		print("Key R pressed!")
	elseif inputObject.KeyCode == Enum.KeyCode.E then
		print("Key E pressed!")
	end
end

tool.Equipped:connect(function(mouse)
	game:GetService("UserInputService").InputBegan:connect(onKeyPress)
end)

1 answer

✓ Accepted answer

That's what the variable gameProcessedEvent is for.

As long as it is false, the player has not focused a TextBox (the object you write stuff in), and so therefore isn't typing:

function onKeyPress(inputObject, gameProcessedEvent)
	if gameProcessedEvent == false then
		-- code
	end
end

Hope I helped!

~TDP

This discussion is preserved as it was. New answers, comments, and votes are closed.