Fixing a Security Camera Script

Hello! I am currently writing a script using a combination of different videos/tutorials to have the players POV go to a security camera when they press E. Currently, I have it working so that when they press E, their POV goes to the security camera. My question is how would I go about making it so when they press E again, their view returns to normal? My code for what I have working is below and I’ve tried to solve my issue using what I currently know but have been unable to. Thanks!

local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local button = game.Workspace.button


--Camera Script when E is pressed with 7 studs of button
UIS.InputBegan:connect(function(keyCode)
	if keyCode.keyCode == Enum.KeyCode.E then 
		if(button.Position - HumanoidRootPart.Position).magnitude < 7 then
			local myCamera = workspace.CurrentCamera
			myCamera.CameraType = Enum.CameraType.Scriptable
			
			local cameraPart = workspace.CameraPart
			
			
			local xTilt = 0
			local xMaxTilt = 2 
			local tiltSpeed = 0.05
			
			while wait() do
				
				myCamera.CFrame = cameraPart.CFrame * CFrame.Angles(0, math.rad(xTilt),0)
				
				xTilt = xTilt + tiltSpeed
				if (math.abs(xTilt) > math.abs(xMaxTilt)) then
					tiltSpeed = tiltSpeed * -1
					xMaxTilt = xMaxTilt * -1
					
				end
			end
		end
	end
end)

1 Like

Use an if statement.

local switch = false
if switch then
    switch = false
    — your code of putting view back to normal.
else
    switch = true
    — your code of putting view to cams.
end

Hope it works!

1 Like

Thanks! I’ll try that out and see what results it gives me. I am having a little trouble figuring out how to return the camera to normal in general though :sweat_smile:

Oh and try to use a remote event, since this is happening locally. But try my previous code first before trying to use remote event.

1 Like

you can use an if statement

also to make it go back to normal you can use : Enum.CameraType.Custom (I think you already know this)

1 Like