Checking if a player is not typing with UIS

I have code for when a player presses a key, it prints something out. But the problem is, I don’t want it to print it out if the player presses the key in the chat.

Code:

local UserInputService = game:GetService("UserInputService")

local function onInputBegan(input, _gameProcessed)
	if input.KeyCode == Enum.KeyCode.P then
		print("The P button has been pressed!")
	end
end

UserInputService.InputBegan:Connect(onInputBegan)

(localscript in starterpack)

This might be able to help

How to tell if a player is typing in the chat

1 Like

Like this?

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local function onInputBegan(input, _gameProcessed)
	if not _gameProcessed then
		print("Player is typing")
	else
	if input.KeyCode == Enum.KeyCode.P then
			print("The P button has been pressed!")
		end
		end
end

It breaks the whole script and never prints out anything anyways.

You added an unnessecary not.

New code:

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local function onInputBegan(input, _gameProcessed)
	if _gameProcessed then
		print("Player is typing")
	elseif input.KeyCode == Enum.KeyCode.P then
		print("The P button has been pressed!")
	end
end

You did not call the function unless there is more to your code than that

There is no function to be called.

Really? Because I thought assigning a function you would have to call it.

There is though. You didn’t connect the local function to anything.

It is correct, the not was nessecary

Did you connect the local function to an event?

Oh, my bad @BitMaster32 and @BitMaster32

No, i did not. What event?

This text will be blurred

Edit: Do i add a remote event then connect it and fire whenever the player presses the button? and there will be a localscript in playerscripts or whatever that prints out the message?

This code also falsely gets the Shift Key Code as a typing variant, so you gotta be careful there.

No worries, sorry for the confusion.

You simply just need to call the function

UserInputService.InputBegan:Connect(onInputBegan)
1 Like

.InputBegan.

New code:

local UserInputService = game:GetService("UserInputService")

local function onInputBegan(input, _gameProcessed)
	if _gameProcessed then
		print("Player is typing")
	elseif input.KeyCode == Enum.KeyCode.P then
		print("The P button has been pressed!")
	end
end

UserInputService.InputBegan:Connect(onInputBegan)
3 Likes

It works, but every time the player types a letter in the chat it prints it out, so now i dont want it to print “player is typing”.

Do i just remove that line?

Edit: yes, i had to delete the line

1 Like

If it works, make sure to set one of the posts to the solution.

Thank you @BitMaster32 and @Katrist for helping me. I can now reference this when I have a similar idea or problem.

Of course! I am glad to help you with this.

2 Likes