How would I check if a player presses a key?

How would I make functions happen when a key is pressed by a player? But for the player to be able to use the keybinds they have to be sitting on a seat. Any ideas how I would check if a key is pressed?
This is a keybind in a module script:

local Keybinds = {
	["Camera Mode"] = Enum.KeyCode.C
	
}

return Keybinds

Now how would I check if a player presses it only when on a specific seat?

7 Likes
local OnSeat = false

--when player sits on a seat change the variable to true (this depends on how u scripted the sitting itself)

--when player gets up turn it to false again

local UIS = game:GetService("UserInputService")
local KeyBind = "C"
UIS.InputBegan:Connect(function(input,Chatting)
	if Chatting then return end -- if player is typing in chat this function will not run
	if not OnSeat then return end --if player is not sitting this function will not run
	if input.KeyCode == Enum.KeyCode[KeyBind] then -- if player pressed C then this function will run
		local whatPlayerPressed = KeyBind
 	end
end)

I didn’t fully understand what you are having troubles with but hope this helps:)

3 Likes

All you need to do is to use the InputObject.Name.

-- Example

-- ModuleScript
local Keybinds = {
      ["Camera_Mode"]  = "C"
}

return Keybinds

-- Local Script
local Keybinds = require(script.KeybindModule) -- Name of your Module to call it.

UserInputService.InputBegan:Connect(function(InputObject, gameProcessedEvent)
     if not gameProcessedEvent then
           local KeyName = InputObject.KeyCode.Name
            if Keybinds["Camera_Mode"] == KeyName then 
            -- Value1 ("Camera_Mode")  == Value2 (KeyName that is "C"), You could also do a inverse operation such as Value2 == Value1, Thats what we are doing.
                
                print("Camera_Mode", keyName)
          end
     end
end)