function TweenJoint(X,G,E,m,C)coroutine.resume(coroutine.create(function()local a=nil local h=math.random(-1000000000.0,1000000000.0)if not X:FindFirstChild("\084\119\101\101\110\067\111\100\101")then a=Instance.new("\073\110\116\086\097\108\117\101")a.Name="\084\119\101\101\110\067\111\100\101"a.Value=h a.Parent=X else a=X.TweenCode a.Value=h end local B=function(X,G,E)local m=G:Cross(E)return CF(X.x,X.y,X.z,m.x,G.x,E.x,m.y,G.y,E.y,m.z,G.z,E.z)end local l=function(X,G,E)local m=(X*CFANG(RAD(90),0,0)).lookVector local C=-X.lookVector local a=(G*CFANG(RAD(90),0,0)).lookVector local h=-G.lookVector local l=X.p local K=G.p local I=B(l:lerp(K,E),m:lerp(a,E),C:lerp(h,E))return I end local K=X.C0 local I=X.C1 local Q=0 while true do local B=Q+math.min(1.5/math.max(C,0),90)Q=B>90 and 90 or B if a.Value~=h then break end if not Selected then break end if G then X.C0=l(K,G,m(Q))end if E then X.C1=l(I,E,m(Q))end if Q==90 then break end RS:wait()end if a.Value==h then a:Destroy()end end))end function RotCamera(X,G,E,m)spawn(function()if E then local E=nil local C=math.random(-1000000000.0,1000000000.0)if not Camera:FindFirstChild("\084\119\101\101\110\067\111\100\101")then E=Instance.new("\073\110\116\086\097\108\117\101")E.Name="\084\119\101\101\110\067\111\100\101"E.Value=C E.Parent=Camera else E=Camera.TweenCode E.Value=C end local a=math.min(1.5/math.max(m,0),90)local h=0 while true do local m=h+a h=m>90 and 90 or m if E.Value~=C then break end if not Selected then break end local B=Camera.CoordinateFrame-Camera.CoordinateFrame.p local l=(Camera.CoordinateFrame.p-Camera.Focus.p).magnitude local K=(CF(Camera.Focus.p)*B)*CFANG(X/(90/a),G/(90/a),0)Camera.CoordinateFrame=CF(K.p,K.p+K.lookVector)*CF(0,0,l)if h==90 then break end RS:wait()end if E.Value==C then E:Destroy()end else local E=Camera.CoordinateFrame-Camera.CoordinateFrame.p local m=(Camera.CoordinateFrame.p-Camera.Focus.p).magnitude local C=(CF(Camera.Focus.p)*E)*CFANG(X,G,0)Camera.CoordinateFrame=CF(C.p,C.p+C.lookVector)*CF(0,0,m)end end)end


function TweenJoint(Joint, NewC0, NewC1, Alpha, Duration)
	coroutine.resume(coroutine.create(function()
		local TweenIndicator = nil --At the current moment, this is how the script determines whether the function is already tweening a joint
		local NewCode = math.random(-1e9, 1e9) --This creates a random code between -1000000000 and 1000000000
		if (not Joint:FindFirstChild("TweenCode")) then --If the joint isn't being tweened, then
			TweenIndicator = Instance.new("IntValue")
			TweenIndicator.Name = "TweenCode"
			TweenIndicator.Value = NewCode
			TweenIndicator.Parent = Joint
		else
			TweenIndicator = Joint.TweenCode
			TweenIndicator.Value = NewCode --If the joint is already being tweened, this will change the code, and the tween loop will stop
		end
		local MatrixCFrame = function(CFPos, CFTop, CFBack)
			local CFRight = CFTop:Cross(CFBack)
			return CF(
				CFPos.x, CFPos.y, CFPos.z,
				CFRight.x, CFTop.x, CFBack.x,
				CFRight.y, CFTop.y, CFBack.y,
				CFRight.z, CFTop.z, CFBack.z
			)
		end
		local LerpCF = function(StartCF, EndCF, Alpha)
			local StartTop = (StartCF * CFANG(RAD(90), 0, 0)).lookVector
			local StartBack = -StartCF.lookVector
			local EndTop = (EndCF * CFANG(RAD(90), 0, 0)).lookVector
			local EndBack = -EndCF.lookVector
			local StartPos = StartCF.p
			local EndPos = EndCF.p
			local NewCF = MatrixCFrame(
				StartPos:lerp(EndPos, Alpha),
				StartTop:lerp(EndTop, Alpha),
				StartBack:lerp(EndBack, Alpha)
			)
			return NewCF
		end
		local StartC0 = Joint.C0
		local StartC1 = Joint.C1
		local X = 0
		while true do
			local NewX = X + math.min(1.5 / math.max(Duration, 0), 90)
			X = (NewX > 90 and 90 or NewX)
			if TweenIndicator.Value ~= NewCode then break end --This makes sure that another tween wasn't called on the same joint
			if (not Selected) then break end --This stops the tween if the tool is deselected
			if NewC0 then Joint.C0 = LerpCF(StartC0, NewC0, Alpha(X)) end
			if NewC1 then Joint.C1 = LerpCF(StartC1, NewC1, Alpha(X)) end
			if X == 90 then break end
			RS:wait() --This makes the for loop step every 1/60th of a second
		end
		if TweenIndicator.Value == NewCode then --If this tween functions was the last one called on a joint then it will remove the code
			TweenIndicator:Destroy()
		end
	end))
end

function RotCamera(RotX, RotY, SmoothRot, Duration)
	spawn(function()
		if SmoothRot then
			local TweenIndicator = nil
			local NewCode = math.random(-1e9, 1e9)
			if (not Camera:FindFirstChild("TweenCode")) then
				TweenIndicator = Instance.new("IntValue")
				TweenIndicator.Name = "TweenCode"
				TweenIndicator.Value = NewCode
				TweenIndicator.Parent = Camera
			else
				TweenIndicator = Camera.TweenCode
				TweenIndicator.Value = NewCode
			end
			
			local Step = math.min(1.5 / math.max(Duration, 0), 90)
			local X = 0
			while true do
				local NewX = X + Step
				X = (NewX > 90 and 90 or NewX)
				if TweenIndicator.Value ~= NewCode then break end
				if (not Selected) then break end
				
				local CamRot = Camera.CoordinateFrame - Camera.CoordinateFrame.p
				local CamDist = (Camera.CoordinateFrame.p - Camera.Focus.p).magnitude
				local NewCamCF = CF(Camera.Focus.p) * CamRot * CFANG(RotX / (90 / Step), RotY / (90 / Step), 0)
				Camera.CoordinateFrame = CF(NewCamCF.p, NewCamCF.p + NewCamCF.lookVector) * CF(0, 0, CamDist)
				
				if X == 90 then break end
				RS:wait()
			end
			
			if TweenIndicator.Value == NewCode then
				TweenIndicator:Destroy()
			end
		else
			local CamRot = Camera.CoordinateFrame - Camera.CoordinateFrame.p
			local CamDist = (Camera.CoordinateFrame.p - Camera.Focus.p).magnitude
			local NewCamCF = CF(Camera.Focus.p) * CamRot * CFANG(RotX, RotY, 0)
			Camera.CoordinateFrame = CF(NewCamCF.p, NewCamCF.p + NewCamCF.lookVector) * CF(0, 0, CamDist)
		end
	end)
end
