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

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

Asked by
gitrog 326 Moderation Voter
6 years ago
Edited 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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

Ad

Answer this question