How do I combine two camera scripts?

I didn’t write either of these scripts and I don’t know how to put scripts together properly so I need some help the first script someone else on the dev forum wrote it for me It makes the camera attach to a part on the iron sights of a gun and the second script I wrote down while following a video Its supposed to be an updated camera but I used it to make the gun rotating in a 360 angle
Both scripts work just fine but together It fights between priority and its very glitchy so if anyone knows how I would combine them thanks

--localscript in the tools Handle
--sightAttachment in the Handle position set 
local UIS = game:GetService("UserInputService")
local UIS = game:GetService("UserInputService")
local RNS = game:GetService("RunService")
local camera = workspace.CurrentCamera

local part = workspace:WaitForChild("Sight")
local sight = part:WaitForChild("sightAttachment")
local originalCFrame = camera.CFrame
local aiming = false

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton2 then aiming = true
		camera.CameraType = Enum.CameraType.Scriptable
		task.spawn(function()
			while aiming do
				camera.CFrame = sight.WorldCFrame
				RNS.Stepped:Wait()
			end
		end)
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then aiming = false
		camera.CameraType = Enum.CameraType.Custom
		camera.CFrame = originalCFrame
	end
end)

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")

local aimingAnim = Instance.new("Animation")
aimingAnim.AnimationId = "rbxassetid://94679025189997"

local aimingTrack = animator:LoadAnimation(aimingAnim)

local userInput = game:GetService("UserInputService")

-- Start animation when RMB is held
userInput.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		if not aimingTrack.IsPlaying then
			aimingTrack:Play()
			aimingTrack.Looped = true -- Ensures animation stays playing
		end
	end
end)

-- Stop animation when RMB is released
userInput.InputEnded:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		if aimingTrack.IsPlaying then
			aimingTrack:Stop()
		end
	end
end)

---------- camera rotation
local UserInputService = game:GetService("UserInputService")
local Runservice = game:GetService("RunService")

local player = game.Players.LocalPlayer

local character = script.Parent
local humanoid = script.Parent:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera

camera.CameraType = Enum.CameraType.Follow
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false
UserInputService.WindowFocused:Connect(function()
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false
end)

local bobSpeed = 0
local bobDistance = 0
local bobWalkMulti = 0

local walkVeloThreshold = 0

local defaultFOV = 0
local targetFOV = 0
local fovChange = 0
local maxFOVvelocity = 60
local FOVsmooth = 0

local sensetivity = .4
local unlockMouseButton = Enum.KeyCode.F
local maxVerticalAngle = math.rad(0)
local camOffset = 6
local turnLeanAmt = 360
local turnLeanSmoothing = 0
local turnLeanMax = 1

local camSmooth = 5

local targetCframe = CFrame.new()
local targetRot = CFrame.Angles(0, 0, 0)
local verticalAngle = 0 
local horizontalAngle = 0
local zAngle = 0

local function UpdateCameraRotation(change)
	local c = change * sensetivity
	horizontalAngle += math.rad(c.x)
	zAngle = math.clamp(zAngle + (c.x / turnLeanAmt), -turnLeanMax, turnLeanMax)
	verticalAngle = math.clamp(verticalAngle + math.rad(c.y), - maxVerticalAngle, maxVerticalAngle)
	
	targetRot = CFrame.Angles(0, horizontalAngle, 0) * CFrame.Angles(verticalAngle, 0, 0)
end

local function UpdateCameraPosition()
    local bobPos = math.sin(tick() * (bobSpeed * bobDistance))	
    if character.PrimaryPart.AssemblyLinearVelocity.Magnitude >= walkVeloThreshold then
   		bobPos = math.sin(tick() * (bobSpeed * bobWalkMulti)) * bobDistance
    end
	local offset = Vector3.new(0, camOffset + bobPos, 0)
	local targetposition = character.PrimaryPart.Position + offset
	
	targetCframe = CFrame.new(targetposition) * targetRot
end

local function SmoothCameraZAxis(deltaTime)
	local amt = deltaTime / turnLeanSmoothing
	
	if zAngle > 0 then
		zAngle = math.max(zAngle - amt, 0)
	else
		zAngle = math.min(zAngle + amt, 0)
	end
	
	local zRotation = CFrame.Angles(0, 0, zAngle)
	targetRot = CFrame.Angles(0, horizontalAngle, 0) * CFrame.Angles(verticalAngle, 0, 0) * zRotation
end	

local function UpdateFOV(deltaTime)
	local velocity = character.PrimaryPart.AssemblyLinearVelocity.Magnitude
	local normalizedVelocity = math.clamp(velocity / maxFOVvelocity, 0 ,1)
	targetFOV = defaultFOV + (normalizedVelocity * fovChange)
	
	camera.FieldOfView = camera.FieldOfView + (targetFOV - camera.FieldOfView) * deltaTime * FOVsmooth
end

UserInputService.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		UpdateCameraRotation(-Vector3.new(input.Delta.X, input.Delta.Y, 0))
	end
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	
	if input.KeyCode == unlockMouseButton then
		if UserInputService.MouseIconEnabled then
			UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
			UserInputService.MouseIconEnabled = false
		else
			UserInputService.MouseBehavior = Enum.MouseBehavior.Default
			UserInputService.MouseIconEnabled = true
			
		end
	end
end)

UserInputService.TouchMoved:Connect(function(touch, processed)
	if processed then return end
	
	UpdateCameraRotation(-Vector3.new(touch.Delta.X, touch.Delta.Y, 0))
end)

Runservice.RenderStepped:Connect(function(deltaTime)
	UpdateCameraPosition()
	SmoothCameraZAxis(deltaTime)
	UpdateFOV(deltaTime)
	
	camera.CFrame = camera.CFrame:Lerp(targetCframe, deltaTime * camSmooth)
	
	local _, camY, _ = camera.CFrame:ToOrientation()
	character:PivotTo(CFrame.new(character.PrimaryPart.Position) * CFrame.Angles(0, camY, 0))
end)

also I’ve tweaked the settings to the second script a lot so its very scuffed

debounce or disconnect first connection when second is used.
Also first script is kinda bad logic wise
task.spawn skips entire resumption cycle every time you use userinput service.

1 Like

How would debounce work? because the second script is an addon to the first

I have a video of what it does I probably should have put in the post

local SecondScriptRunning = true
--First script
if SecondScriptRunning then return end

Alternatively make it disconnect

1 Like

Ok I’ve got an idea of what to do now ill reply again if something doesn’t work