Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

UserInputService doesn't want to work, why?

Hi, I've been using UserInputService for a long time with no problems, only my script doesn't want to work now. My script's location is in StarterPlayer.StarterPlayerScripts, also it's a local script.

local Players = game:GetService("Players")

local Player = Players.LocalPlayer

local UserInputService = game:GetService("UserInputService")

function onInputBegan(input, gameProcessed)
	
	if gameProcessed then
		
		print(gameProcessed)
		
		return
	end
	
	if input == Enum.KeyCode.V then
		
		print(input)
		
		if Player.CameraMode == Enum.CameraMode.Classic then
			
			Player.CameraMode = Enum.CameraMode.LockFirstPerson
			
		else
			
			Player.CameraMode = Enum.CameraMode.Classic
		end
	end
end

UserInputService.InputBegan:Connect(onInputBegan)

1 answer

✓ Accepted answer

The issue here is that you are comparing the input object with a KeyCode. You can find the KeyCode inside the input, simply by referencing it.

The code below should work.

local Players = game:GetService("Players")

local Player = Players.LocalPlayer

local UserInputService = game:GetService("UserInputService")

function onInputBegan(input, gameProcessed)

	if gameProcessed then

		print(gameProcessed)

		return
	end
	
	if input.KeyCode == Enum.KeyCode.V then

		print(input)

		if Player.CameraMode == Enum.CameraMode.Classic then

			Player.CameraMode = Enum.CameraMode.LockFirstPerson

		else

			Player.CameraMode = Enum.CameraMode.Classic
		end
	end
end

UserInputService.InputBegan:Connect(onInputBegan)

1 comment

0

Thanks, I had a look through this.

This discussion is preserved as it was. New answers, comments, and votes are closed.