FOV script works in studio but not in game?

I encountered a bug where my weapon zoom script doesn’t work at all in game, but it works perfectly in my studio. In studio, I added print(“Success”) & it works, and so does the script, but in game there is no errors or print when I check the console.

My script is:

PMouse.Button2Down:connect(function()
	local Camera = game.Workspace.CurrentCamera
	if Equipped == true and Camera.FieldOfView == 40 then
		Camera.FieldOfView = 70 else
		if Equipped == true and Camera.FieldOfView == 70 then
			Camera.FieldOfView = 40
			print("Success")
		end
	end
end)

Thanks.

Have you checked that all the conditional statements are working properly in both Studio and in game? I would recommend either printing each condition before the conditional statement or including else statements with unique prints so you can tell which direction your code is taking. Specifically, I’m looking at the ‘Equipped’ variable here, as its scope appears to fall outside the code you’ve cited.

–East98

When I removed “Equipped == true” to check if that was the problem. The same thing happens. The FOV change would only work in studio but not in the game. The only time It works in the game is if you make it super simple without if statements.

Did you remove the ‘Equipped == true’ for both cases? Perhaps a bit more of your code is needed…

I created a local script in a blank game & put this script into startercharacter*:

Player = game.Players.LocalPlayer
PMouse = Player:GetMouse()

PMouse.Button2Down:connect(function()
	local Camera = game.Workspace.CurrentCamera
	if Camera.FieldOfView == 70 then
		Camera.FieldOfView = 40
	else
		if Camera.FieldOfView == 40 then
			Camera.FieldOfView = 70
		end
	end
end)

I tested it in studio & it worked, but not in game…

edit: I meant starterchar not starterplr, so try it with starterchar…

It works as is in both cases for me. One issue may be that the method your’e using to get the Mouse has been superseded by UserInputService and ContextActionService. Maybe try using one of those services to bind the event. Also, it would be better practice to store some variable to determine whether the camera is currently in the “zoom” state instead of checking for equality of a certain FOV. Your code may look something like:

local userInputService = game:GetService("UserInputService")

local ZOOM = false

userInputService.InputBegan:connect(function(io, gp)
	--io (short for InputObject)
	--gp (short for GameProcessed)
	if not gp then
		if io.UserInputType == Enum.UserInputType.MouseButton2 then
			ZOOM = not ZOOM
			if ZOOM then
				workspace.CurrentCamera.FieldOfView = 40
			else
				workspace.CurrentCamera.FieldOfView = 70
			end
		end
	else
		print("GP")
	end
end)
1 Like

Thank you for your input,

Is there a roblox article I can ready that covers this so I can learn more?

Just a few links off the top of my head…

–East98

1 Like