Issue with 'E' Keycode Detection

Hello! I need a hand; when I press the “E” keycode, nothing happens. What is wrong? I’ve tried multiple ways, and nothing seems to work.

Module Script (replicatedstorage):

-- CameraModuleScript

local CameraModule = {}

function CameraModule.setupCamera(character, camera, forwardOffset, upwardOffset)
	local head = character:WaitForChild("Head")

	local function updateCameraPosition()
		local offset = Vector3.new(0, upwardOffset, forwardOffset)
		camera.CFrame = CFrame.new(head.Position + offset)
	end

	-- Set the initial camera position
	updateCameraPosition()

	-- Continuously update the camera position to follow the head
	head:GetPropertyChangedSignal("Position"):Connect(updateCameraPosition)

	local isThirdPerson = false

	local function toggleCameraView()
		isThirdPerson = not isThirdPerson
		if isThirdPerson then
			-- Change to third-person view
			camera.CameraType = Enum.CameraType.Scriptable
		else
			-- Change back to first-person view
			camera.CameraType = Enum.CameraType.Custom
		end
	end

	local function setupInput()
		local player = game.Players.LocalPlayer
		local mouse = player:GetMouse()

		-- Bind the toggle function to the "E" key using KeyDown
		mouse.KeyDown:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.E then
				toggleCameraView()
			end
		end)
	end

	setupInput()
end

return CameraModule

Localscript (starter player folder):

-- LocalScript

local CameraModule = require(game.ReplicatedStorage:WaitForChild("CameraSwitchModule"))

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = game.Workspace.CurrentCamera

-- Adjust these variables for customization
local forwardOffset = 3
local upwardOffset = 1.5

-- Use the function from the module to set up the camera
CameraModule.setupCamera(character, camera, forwardOffset, upwardOffset)

project organization:

image

The issue is that, the toggleView change doesn’t work when I press the E key, and in addition to this, the variables

-- Adjust these variables for customization
local forwardOffset = 3
local upwardOffset = 1.5

it don’t make any changes. What is wrong? :frowning:

I think that’s because you’re using mouse as the key down. You should change it to

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.E then
		toggleCameraView()
	end
end)

In order for key inputs to register, you have to require the UserInputService.

local UIS = game:GetService("UserInputService")

Then you proceed with this code;

In order to get the keyboard input, you have to collect the event that happens when you press a key on your keyboard.

UIS.InputBegan:Connect:(function(input)
    if input.KeyCode == Enum.KeyCode.E then
		toggleCameraView()
        ...
1 Like

Mouse.KeyDown does not return keycodes, it returns strings.

So you would have to replace that, with this:

if input == "e" then

Thank you very much for your comments people! This was very helpful, yes, I have a problem when putting the third person and that is that the camera is deconfigured:

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