If-statement does not properly recognize input from User Input Service

Hi,

My goal with the program was to increase FOV and change the cursor when right click is held, and revert it back when it is released to create an aiming effect with a gun. I did this using User Input Service’s InputBegan and InputEnded events. It works fine with InputBegan, but when I release right click, nothing happens. Using breakpoints I determined that it does register, however the if-statement says that the input argument is not right click, which is weird. Here is my code:

Code for input begin (this part works)

--v This code works fine v
UIS.InputBegan:Connect(function(input, gameHandledEvent)
	if gameHandledEvent or not expectingInput then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		mouseDown = true
		
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
		aiming = true
		UpdateMouseIcon()
		workspace.CurrentCamera.FieldOfView = 60
    end
end)

Code for input ended (does not work)

--v This is where the issue is v
UIS.InputEnded:Connect(function(input, gameHandledEvent)
	if gameHandledEvent then return end
	--The left click release for shooting works
	if input == Enum.UserInputType.MouseButton1 then
		mouseDown = false
	--The right click release for aiming does not get past this if-statement	
	elseif input == Enum.UserInputType.MouseButton2 then
		aiming = false
		UpdateMouseIcon()
		workspace.CurrentCamera.FieldOfView = 70
	end
end)

Any help/solutions would be greatly appreciated!

Shouldn’t both of these be changed to input.UserInputType instead of just input?

oh gee :man_facepalming: how could I not see that. Thanks for the help I have no idea how the shooting part worked in the first place with this error.

Functions inside :Connect() are called on seperate threads so they won’t error in the main thread. Therefore the script still worked fine just not the associated function :wink:

1 Like

Thanks for the help even though this was such a silly mistake