Security Camera Int Value multiplying? Help?

I’m back once again. This time is something i have no control over (at least i don’t think it is). Im a pretty lousy coder so any info is appreciated. But here is my issue, sorry im ranting.

I have a security system that works quite nicely. When you enter the cameras it takes your camera and places it inside the security cams around my level. If you press → (Right) or ← (Left) arrows on the keyboard It will add a 1 to an int value (between 0 and 21). Once on the number a while wait loop will then see the int number and snap you to the corresponding camera (I’ll provide the whole script for anyone who wishes to copy. Its not crazy amazing lol(some of the code was simplified but im sure yall will understand)). When you press the down arrow you are then released from the restricted cams and the int value gets set to 0.

The true problem lies here. When i enter the security cams again, and press arrow. It for some reason adds 2 instead of +1… so if i was on cam 1 it would jump to 3, 5, 7, 9 etc. Or if i leave again and re-enter it will then add 3 the next round (1,4,7,10 etc) AND I DON’T KNOW WHY? my code is probably a mess but literally ANY INFO is hella appreciated because I swear imma rip my hair out.

TL;DR – Camera seems to add an extra number each time I press the left or right arrows upon leaving and re-entering the security camera.

Code:

local Camera = game.Workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local Folder = game.Workspace.SecurityCams
local CamGUI = game.StarterGui.SecurityButtons

local Cam1 = Folder.SecurityCamA.Viewport
local Cam2 = Folder.SecurityCamB.Viewport
local Cam3 = Folder.SecurityCamC.Viewport
--- Removed for repetition but from Cam 4 - 19 its the same.
local Cam20 = Folder.SecurityCamT.Viewport
local Cam21 = Folder.SecurityCamU.Viewport


local player = game.Players.LocalPlayer
local CamInstance = Instance.new("NumberValue")
CamInstance.Parent = player
CamInstance.Value = 0

local A = false

game.ReplicatedStorage.SecurityCameraEvent.OnClientEvent:Connect(function()
	A = false
	CamInstance.Value = 0
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = Cam1.CFrame
	print(""..player.Name.." is on Cam 1")
	
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Right then
			CamInstance.Value = CamInstance.Value + 1
		end
		
	end)
	
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Left then
			CamInstance.Value = CamInstance.Value - 1
		end

	end)
	
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Down then
			Camera.CameraType = Enum.CameraType.Custom
			CamInstance.Value = 0
			
			A = true
			if A == false then
				Camera.CameraType = Enum.CameraType.Custom
				CamInstance.Value = 0
			end
		end

	end)
	
	if A == false then
		while wait(.1) do	
			
			if CamInstance.Value == 1 then
				Camera.CFrame = Cam1.CFrame
				print(""..player.Name.." is on Cam 1")
			end
			
			if CamInstance.Value == 2 then
				Camera.CFrame = Cam2.CFrame
				print(""..player.Name.." is on Cam 2")
			end
			
			if CamInstance.Value == 3 then
				Camera.CFrame = Cam3.CFrame
				print(""..player.Name.." is on Cam 3")
			end
			
			--- Removed for repetition but from 4 - 19 its the same.
			
			if CamInstance.Value == 20 then
				Camera.CFrame = Cam20.CFrame
				print(""..player.Name.." is on Cam 20")
			end
			
			if CamInstance.Value == 21 then
				Camera.CFrame = Cam21.CFrame
				print(""..player.Name.." is on Cam 21")
			end
		
			if CamInstance.Value >= 22 then
				Camera.CFrame = Cam1.CFrame
				CamInstance.Value = 0
				print(""..player.Name.." is at end of cam feed. Redirecting to cam 1")
			end
		
			if CamInstance.Value <= -1 then
				Camera.CFrame = Cam1.CFrame
				CamInstance.Value = 21
				print(""..player.Name.." is at start of cam feed. Redirecting to cam 21")
			end
		end
	end		
	UIS.InputBegan:Connect(function(input)
		if CamInstance.Value >= 22 then
			CamInstance.Value = 0
			print(CamInstance.Value)
		end
	end)
end)

It’s a long read and I truly apologize but im honestly so lost. I clearly bit off WAY too much than i can chew…

Thank you anyone who is able to help! I really appreciate it :*)

The problem with your script is that everytime you leave the camera, the inputbegan doesn’t get disconnected so essentially once you go back, it’ll run twice and so adding 2 instead of 1

oh yeah you can also combine all of the Inputbegan into a singular function so instead of

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.Keycode.Q then
      print("real")
   end
end)

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.Keycode.E then
      print("fake")
   end
end)

you can just do

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.Keycode.Q then
      print("real")
   elseif input.KeyCode == Enum.Keycode.E then
      print("fake")
   end
end)
1 Like

OH my god I see, I understand now! Thank you so much I really appreciate this! May I ask one final question tho? how would I go about disconnecting the Input? I know it must involve something along the lines of-

UIS.InputEnded:Connect(function(input)
--Code etc etc.
end)

right? is that where I would put the code to disconnect you from the camera? or does the Input Ended go basically at the end of the script?

To disconnect a function you would do something like this:

-- Make a variable for it
local thing = UIS.InputEnded:Connect(function(input)

end)

--Disconnect it
thing:Disconnect()
1 Like

Ah okie, So i think I understand, i edited the code to look like this (it doesn’t work still but i wanna know your opinion if Im doing it right) and thank you so much as well! i really appreciate it :*D

UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Right then
			CamInstance.Value = CamInstance.Value + 1
		elseif input.KeyCode == Enum.KeyCode.Left then
			CamInstance.Value = CamInstance.Value - 1
		end
		
		DisconectCams:Disconnect()
			
	end)