roblox key pressed script

-- Step 1: Get the player and the player's input service
local player = game.Players.LocalPlayer
local inputService = game:GetService("UserInputService")

-- Step 2: Define a function to handle key presses
local function onKeyPress(input, gameProcessedEvent)
    -- Step 3: Check if the input is a key press and not processed by the game
    if input.UserInputType == Enum.UserInputType.Keyboard and not gameProcessedEvent then
        -- Step 4: Check if the pressed key is the desired key (e.g., 'E' key)
        if input.KeyCode == Enum.KeyCode.E then
            -- Step 5: Add the desired actions to perform when the key is pressed
            print("E key pressed!") -- Replace this with your desired code
        end
    end
end

-- Step 6: Connect the function to the key press event
inputService.InputBegan:Connect(onKeyPress)