--------------------[ ADS FUNCTIONS ]-------------------------------------------------

function AimGun()
	if (Camera.CoordinateFrame.p - Camera.Focus.p).magnitude > 1 or Reloading or Knifing or ThrowingGrenade then return end
	local Scope = Gui_Clone:WaitForChild("Scope"):WaitForChild("Img")
	local Steady = Gui_Clone:WaitForChild("Scope"):WaitForChild("Steady")
	Aimed = true
	Aiming = true
	CurrentRecoil = S.Recoil.Aimed
	CurrentSpread = S.Spread.Aimed
	Gui_Clone.CrossHair.Box:TweenSizeAndPosition(
		UDim2.new(),
		UDim2.new(),
		Enum.EasingDirection.Out,
		Enum.EasingStyle.Sine,
		S.AimAnimation and S.AimSpeed or 0,
		true
	)
	if S.AimAnimation then
		local CurrentFOV = Camera.FieldOfView
		local CurrentCameraOffset = Humanoid.CameraOffset
		local CurrentTrans = Scope.BackgroundTransparency
		TweenJoint(LWeld, ArmC0[1], S.ArmC1_Aimed.Left, Sine, S.AimSpeed)
		TweenJoint(RWeld, ArmC0[2], S.ArmC1_Aimed.Right, Sine, S.AimSpeed)
		TweenJoint(AnimWeld, CF(0, 1, 0), CF(), Sine, S.AimSpeed)
		TweenJoint(Grip, Grip.C0, Aimed_GripCF, Sine, S.AimSpeed)
		TweenJoint(LWeld2, CF(), CF(), Sine, S.AimSpeed)
		TweenJoint(RWeld2, CF(), CF(), Sine, S.AimSpeed)
		local X = 0
		while true do
			local NewX = X + 1.5 / S.AimSpeed
			X = (NewX > 90 and 90 or NewX)
			if (not Aimed) then break end
			if (not Selected) then break end
			local Alpha = 1 - COS(RAD(X))
			Camera.FieldOfView = NumLerp(CurrentFOV, S.MaxZoom, Alpha)
			if S.GuiScope then
				Scope.BackgroundTransparency = NumLerp(CurrentTrans, 0, Alpha)
			end
			if X == 90 then break end
			RS:wait()
		end
	else
		LWeld.C0, LWeld.C1 = ArmC0[1], S.ArmC1_Aimed.Left
		RWeld.C0, RWeld.C1 = ArmC0[2], S.ArmC1_Aimed.Right
		AnimWeld.C0 = CF(0, 1, 0)
		Grip.C1 = Aimed_GripCF
		LWeld2.C1, RWeld2.C1 = CF(), CF()
		Camera.FieldOfView = S.MaxZoom
	end
	Aiming = (not Aimed)
	if (not Aiming) and S.GuiScope then
		spawn(function()
			Steady.Visible = true
			Scope.BackgroundTransparency = 1
			Scope.ImageTransparency = 0
			
			local Gun_Model = Instance.new("Model")
			Gun_Model.Name = "Gun_Model"
			for _, Obj in pairs(Gun:GetChildren()) do
				if Obj:IsA("BasePart") then
					local ObjClone = Obj:Clone()
					ObjClone.Parent = Gun_Model
					
					local W = Instance.new("Weld")
					W.Part0 = ObjClone
					W.Part1 = Obj
					W.Parent = ObjClone
					
					local PrevTrans = Instance.new("NumberValue")
					PrevTrans.Name = "PrevTrans"
					PrevTrans.Value = Obj.Transparency
					PrevTrans.Parent = Obj
					
					Obj.Transparency = 1
				end
			end
			Gun_Model.Parent = Character
		end)
		spawn(function()
			while Aimed do
				local Ang = (CameraSteady and 0 or (Idleing and AnimAng[2] or AnimAng[4]))
				if CameraSway ~= 0 then
					local OffsetX, OffsetY = 0, 0
					if Idleing then
						OffsetX = (RAD(COS(Ang * 5 * (CameraSway ^ 0.4))) / 40) * (CameraSway ^ 1.2) * StanceSway * S.ScopeSway
						OffsetY = (RAD(COS(Ang * 2 * (CameraSway ^ 0.4))) / 40) * (CameraSway ^ 1.2) * StanceSway * S.ScopeSway
					else
						OffsetX = (RAD(COS(Ang * 3/2 * (CameraSway ^ 0.4))) / 25) * (CameraSway ^ 1.2) * StanceSway * S.ScopeSway
						OffsetY = (RAD(COS(Ang * 3/5 * (CameraSway ^ 0.4))) / 25) * (CameraSway ^ 1.2) * StanceSway * S.ScopeSway
					end
					RotCamera(OffsetX, OffsetY)
				end
				RS:wait()
			end
		end)
	end
end

function UnAimGun(Exception)
	local Scope = Gui_Clone:WaitForChild("Scope"):WaitForChild("Img")
	local Steady = Gui_Clone:WaitForChild("Scope"):WaitForChild("Steady")
	if (not Exception) then
		if (not Aimed) then return end
		if (Reloading and Exception) or Knifing and (not ThrowingGrenade) then return end
		CurrentRecoil = S.Recoil.Hipfire
		CurrentSpread = ((Idleing and (not Walking)) and S.Spread.Hipfire or S.Spread.Walking)
		if Walking and Aimed then
			Gui_Clone.CrossHair.Box:TweenSizeAndPosition(
				UDim2.new(0, 150, 0, 150),
				UDim2.new(0, -75, 0, -75),
				Enum.EasingDirection.Out,
				Enum.EasingStyle.Linear,
				S.AimAnimation and S.AimSpeed or 0,
				true
			)
		elseif Idleing then
			Gui_Clone.CrossHair.Box:TweenSizeAndPosition(
				UDim2.new(0, 70, 0, 70),
				UDim2.new(0, -35, 0, -35),
				Enum.EasingDirection.Out,
				Enum.EasingStyle.Linear,
				S.AimAnimation and S.AimSpeed or 0,
				true
			)
		end
		Aimed = false
		Aiming = true
		if S.GuiScope then
			spawn(function()
				local Gun_Model = Character:FindFirstChild("Gun_Model")
				if Gun_Model then
					Gun_Model:Destroy()
				end
				
				for _, Obj in pairs(Gun:GetChildren()) do
					if Obj:IsA("BasePart") then
						local PrevTrans = Obj:FindFirstChild("PrevTrans")
						
						Obj.Transparency = PrevTrans.Value
						PrevTrans:Destroy()
					end
				end
			end)
		end
		if S.AimAnimation then
			local CurrentFOV = Camera.FieldOfView
			local CurrentCameraOffset = Humanoid.CameraOffset
			local CurrentTrans = (Scope.BackgroundTransparency == 1 and (S.GuiScope and 0 or 1) or Scope.BackgroundTransparency)
			Scope.ImageTransparency = 1
			Steady.Visible = false
			TweenJoint(LWeld, ArmC0[1], S.ArmC1_UnAimed.Left, Sine, S.AimSpeed)
			TweenJoint(RWeld, ArmC0[2], S.ArmC1_UnAimed.Right, Sine, S.AimSpeed)
			TweenJoint(Grip, Grip.C0, CFANG(0, RAD(20), 0), Sine, S.AimSpeed)
			if S.PlayerAnimations then
				TweenJoint(LWeld2, CF(), CFANG(0, RAD(ArmTilt), 0), Sine, S.AimSpeed)
				TweenJoint(RWeld2, CF(), CFANG(0, RAD(ArmTilt), 0), Sine, S.AimSpeed)
			end
			local X = 0
			while true do
				local NewX = X + 1.5 / S.AimSpeed
				X = (NewX > 90 and 90 or NewX)
				if Aimed then break end
				if (not Selected) then break end
				local Alpha = 1 - COS(RAD(X))
				Camera.FieldOfView = NumLerp(CurrentFOV, 90, Alpha)
				Scope.BackgroundTransparency = NumLerp(CurrentTrans, 1, Alpha)
				if X == 90 then break end
				RS:wait()
			end
		else
			Scope.BackgroundTransparency = 1
			Scope.ImageTransparency = 1
			Steady.Visible = false
			LWeld.C0, LWeld.C1 = ArmC0[1], S.ArmC1_UnAimed.Left
			RWeld.C0, RWeld.C1 = ArmC0[2], S.ArmC1_UnAimed.Right
			Grip.C1 = CFANG(0, RAD(20), 0)
			LWeld2.C1, RWeld2.C1 = CFANG(0, RAD(ArmTilt), 0), CFANG(0, RAD(ArmTilt), 0)
			Camera.FieldOfView = 90
		end
		Aiming = Aimed
	else
		if S.GuiScope then
			spawn(function()
				local Gun_Model = Character:FindFirstChild("Gun_Model")
				if Gun_Model then
					Gun_Model:Destroy()
				end
				
				for _, Obj in pairs(Gun:GetChildren()) do
					if Obj:IsA("BasePart") then
						local PrevTrans = Obj:FindFirstChild("PrevTrans")
						
						Obj.Transparency = PrevTrans.Value
						PrevTrans:Destroy()
					end
				end
			end)
		end
		spawn(function()
			Aimed = false
			Aiming = false
			CurrentRecoil = S.Recoil.Hipfire
			CurrentSpread = ((Idleing and (not Walking)) and S.Spread.Hipfire or S.Spread.Walking)
			if (not Walking) then
				Gui_Clone.CrossHair.Box:TweenSizeAndPosition(
					UDim2.new(0, 70, 0, 70),
					UDim2.new(0, -35, 0, -35),
					Enum.EasingDirection.Out,
					Enum.EasingStyle.Linear,
					S.AimAnimation and S.AimSpeed or 0,
					true
				)
			else
				Gui_Clone.CrossHair.Box:TweenSizeAndPosition(
					UDim2.new(0, 150, 0, 150),
					UDim2.new(0, -75, 0, -75),
					Enum.EasingDirection.Out,
					Enum.EasingStyle.Linear,
					S.AimAnimation and S.AimSpeed or 0,
					true
				)
			end
			local CurrentFOV = Camera.FieldOfView
			local CurrentCameraOffset = Humanoid.CameraOffset
			local CurrentTrans = (Scope.BackgroundTransparency == 1 and (S.GuiScope and 0 or 1) or Scope.BackgroundTransparency)
			Scope.ImageTransparency = 1
			Steady.Visible = false
			if LWeld:FindFirstChild("TweenCode") then LWeld.TweenCode:Destroy() end
			if RWeld:FindFirstChild("TweenCode") then RWeld.TweenCode:Destroy() end
			if Grip:FindFirstChild("TweenCode") then Grip.TweenCode:Destroy() end
			if LWeld2:FindFirstChild("TweenCode") then LWeld2.TweenCode:Destroy() end
			if RWeld2:FindFirstChild("TweenCode") then RWeld2.TweenCode:Destroy() end
			if S.AimAnimation then
				local X = 0
				while true do
					local NewX = X + 1.5 / S.AimSpeed
					X = (NewX > 90 and 90 or NewX)
					if Aimed then break end
					if (not Selected) then break end
					local Alpha = 1 - COS(RAD(X))
					Camera.FieldOfView = NumLerp(CurrentFOV, 90, Alpha)
					Scope.BackgroundTransparency = NumLerp(CurrentTrans, 1, Alpha)
					if X == 90 then break end
					RS:wait()
				end
			else
				Scope.BackgroundTransparency = 1
				Scope.ImageTransparency = 1
				Steady.Visible = false
				Camera.FieldOfView = 90
			end
		end)
	end
end