How do I ignore KeyCode input from a player while they are chatting?

I have a plane system that uses KeyCodes to determine when a player flying the plane wants to speed up, slow down, turn on/off the engine, etc.

An issue I have encountered is, if I am typing in chat, and there is a word/phrase/sentence that uses the same letters used to control the planes, those keys will fire, even though I am only typing and do not wish to do that action.

How do I prevent KeyCode actions from being fired/acted on when a player is chatting?

Here is my code:

USINS.InputBegan:connect(function(input)
	local code=input.KeyCode
	if code==Enum.KeyCode.S or code==Enum.KeyCode.DPadDown then
		remote:InvokeServer("-") --- slow down
	elseif code==Enum.KeyCode.W or code==Enum.KeyCode.DPadUp then
		remote:InvokeServer("+") --- speed up
	elseif code==Enum.KeyCode.Y or input.KeyCode == Enum.KeyCode.ButtonY then
		remote:InvokeServer("engine") --- toggle engine
	end	
end)
2 Likes

Try:

USINS.InputBegan:connect(function(input, gameProcessed)
    if (gameProcessed) then
        return
    end
	local code=input.KeyCode
	if code==Enum.KeyCode.S or code==Enum.KeyCode.DPadDown then
		remote:InvokeServer("-") --- slow down
	elseif code==Enum.KeyCode.W or code==Enum.KeyCode.DPadUp then
		remote:InvokeServer("+") --- speed up
	elseif code==Enum.KeyCode.Y or input.KeyCode == Enum.KeyCode.ButtonY then
		remote:InvokeServer("engine") --- toggle engine
	end	
end)
2 Likes

Yep, that fixed it. Thank you!

1 Like

Why did you do (gameProcessed)?
Why is it in parenthesis?

1 Like

It’s just in the style I write in. It doesn’t do anything, just makes it easier for me to read. And also because of how I write in different languages such as C#.

Oh okay, I thought it changed something in the code :sweat_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.