Clicking E to show Inventory

I made this LocalScript Recently:

function FireServer()
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local remoteEvent = ReplicatedStorage:WaitForChild("OpenBackpackOnClick")
    remoteEvent:FireServer()
end

game:GetService("UserInputService").InputBegan:Connect(function(key)
 	if key.KeyCode == Enum.KeyCode.E then
		FireServer()
	end
end)

And I want Player to only be able to open Backpack when they’re not chatting. However I’m not sure how to do it. Can someone help me? Thanks!

Here’s the Article that may help you:

2 Likes

The InputBegan event has a second argument sent. Your first one is key (as you defined this), thus whatever your second one will be is what that argument is.

That argument/parameter is commonly referred to as “gameProcessed” which is whether the action was involved with the physical game world or not. It is a boolean value.

If the boolean value is true, it means the player interacted with the physical world in the game and if not, they’re on something that isn’t which can be GUI and the chat is a GUI.

Your answer? Make a check to make sure gameProcessed is true and not false.

An alternative is to use ContextActionService function binding which automatically ignores GUI for you and can even have created mobile buttons with it.

gameProcessed solution implementation
game:GetService("UserInputService").InputBegan:Connect(function(key, gameProcessed)
    if not gameProcessed then return end -- this is known as a guard clause
 	if key.KeyCode == Enum.KeyCode.E then
		FireServer()
	end
end)
ContextActionService alternative
local actionName = "inventory"
game:GetService("ContextActionService"):BindAction(actionName,
    function(bindName, inputState, inputObject) -- parameters not really relevant
        FireServer() -- do whatever you want
    end,
    false, -- whether to create a mobile button
    Enum.KeyCode.E -- you can include more keycodes, input state enums as well if you want
)
1 Like

UserInputService.InputBegan:Connect(function(InoutObject, GameProcessed)
if (InputObject.UserInputType = Enum.UserInputType.KeyBoard) then
if (GameProcessed) then --// Otherwise known as ‘IsTyping’
print(“Is Typing!”)
end
end
end)

I didnt make this. But this works.

Check if the game is processed aka (Plr is Chatting). And if it is processed then return so it doesn’t execute the remote.

game:GetService("UserInputService").InputBegan:Connect(function(key, processed)
if processed  then return end
if key.KeyCode == Enum.KeyCode.E then
FireServer()
end
end)

Also I don’t recommend firing a remote to open a backpack…

3 Likes

Special thanks to @ArtFoundation and @ScriptingVoid. Thanks for the help! If I could I would mark your replies as a Solution(s).

1 Like