How can I fix this camera bug?

So I am making a mounted machine gun for my game but the camera like while the player is controlling it, and after you come off of it you cant move your camera anymore.

Video:

Code: (Relevant part)

RemoteEvent3.OnClientEvent:Connect(function(Browning)
	if Browning ~= nil then
		Mouse.Button2Down:Connect(function()
			Button2Stats = "Down"
			
			Mouse.Move:Connect(function()
				if Button2Stats == "Down" then
					local IsControlling = Browning.IsControlling
					
					if IsControlling.Value == true then
						local Camera = game.Workspace.CurrentCamera
						Camera.CameraType = Enum.CameraType.Scriptable
						Camera.CFrame = Browning.BrowningCam2.CFrame
						Camera.FieldOfView = 40
					end
				end
			end)
		end)

		Mouse.Button2Up:Connect(function()
			Button2Stats = "Up"
			
			Mouse.Move:Connect(function()
				if Button2Stats == "Up" then
					local IsControlling = Browning.IsControlling

					if IsControlling.Value == true then
						local Camera = game.Workspace.CurrentCamera
						Camera.CameraType = Enum.CameraType.Scriptable

						Mouse.Move:Connect(function()
							Camera.CFrame = Browning.BrowningCam.CFrame
							Camera.FieldOfView = 60
						end)
					else
						local Camera = game.Workspace.CurrentCamera
						Camera.CameraSubject = Player.Character:WaitForChild("Humanoid")
						Camera.FieldOfView = 70
						Camera.CameraType = Enum.CameraType.Custom
					end
				end
			end)
		end)
	end
end)

One of these values must be incorrect:

IsControlling.Value
Button2Stats 
Browning 

Try adding some prints and breakpoints to see what these values are.

I added prints to the lines and they print what they should be printing, and there are no errors.

Try adding Pcalls to detect any errors in the programm

Would it be because the Mouse.Move function is still functioning even after the Camera is reset?

What you can do is simply disconnect the Mouse functions when you do not need it anymore (eg. When the camera is reset)

Like this:

local mouseConnection1
local mouseConnection2

mouseConnection1 = Mouse.Button2Down:Connect(function()
	Button2Stats = "Down"

	Mouse.Move:Connect(function()
		if Button2Stats == "Down" then
			local IsControlling = Browning.IsControlling

			if IsControlling.Value == true then
				local Camera = game.Workspace.CurrentCamera
				Camera.CameraType = Enum.CameraType.Scriptable
				Camera.CFrame = Browning.BrowningCam2.CFrame
				Camera.FieldOfView = 40
			end
		end
	end)
end)

mouseConnection2 = Mouse.Button2Up:Connect(function()
	Button2Stats = "Up"
	
	Mouse.Move:Connect(function()
		if Button2Stats == "Up" then
			local IsControlling = Browning.IsControlling

			if IsControlling.Value == true then
				local Camera = game.Workspace.CurrentCamera
				Camera.CameraType = Enum.CameraType.Scriptable

				Mouse.Move:Connect(function()
					Camera.CFrame = Browning.BrowningCam.CFrame
					Camera.FieldOfView = 60
				end)
			else
				local Camera = game.Workspace.CurrentCamera
				Camera.CameraSubject = Player.Character:WaitForChild("Humanoid")
				Camera.FieldOfView = 70
				Camera.CameraType = Enum.CameraType.Custom
				mouseConnection1:Disconnect()
				mouseConnection2:Disconnect()
			end
		end
	end)
end)

Due to the nature of the Mouse functions creating itself via OnClientEvent, the Mouse functions will initialize automatically so no need to worry when disconnecting the connections as a whole. (This also combats memory leaking due to unused connections, which is always nice!)

It didn’t change anything, however, I was able to come up with this which did fix the bug where after coming off the weapon;

RemoteEvent3.OnClientEvent:Connect(function(Browning)
	if Browning ~= nil then
		Mouse.Button2Down:Connect(function()
			Button2Stats = "Down"
			if Button2Stats == "Down" then
				local IsControlling = Browning.IsControlling
				
				if IsControlling.Value == true then
					local Camera = game.Workspace.CurrentCamera
					Camera.CameraType = Enum.CameraType.Scriptable
					Camera.CameraSubject = Browning.BrowningCam2
					Camera.FieldOfView = 40
				end
			end
		end)
		
		Mouse.Button2Up:Connect(function()
			Button2Stats = "Up"
			
			if Button2Stats == "Up" then
				local IsControlling = Browning.IsControlling
				print(IsControlling.Value)
				
				if IsControlling.Value == true then
					local Camera = game.Workspace.CurrentCamera
					Camera.CameraType = Enum.CameraType.Scriptable
					Camera.CameraSubject = Browning.BrowningCam
					Camera.FieldOfView = 60
				else
					local Camera = game.Workspace.CurrentCamera
					Camera.CameraSubject = Player.Character:WaitForChild("Humanoid")
					Camera.FieldOfView = 70
					Camera.CameraType = Enum.CameraType.Custom
					print(Camera)
				end
			end
		end)
	end
end)

Although, now the camera won’t even go to the part which it is supposed to. Here is a video;

Could you inform me as to what a Pcall is and how to use it?

This guide should help

1 Like