function TweenJoint(u,q,h,S,Y)coroutine.resume(coroutine.create(function()local p=nil local t=math.random(-1000000000.0,1000000000.0)if not u:FindFirstChild("\084\119\101\101\110\067\111\100\101")then p=Instance.new("\073\110\116\086\097\108\117\101")p.Name="\084\119\101\101\110\067\111\100\101"p.Value=t p.Parent=u else p=u.TweenCode p.Value=t end local e=function(u,q,h)local S=q:Cross(h)return CF(u.x,u.y,u.z,S.x,q.x,h.x,S.y,q.y,h.y,S.z,q.z,h.z)end local j=function(u,q,h)local S=(u*CFANG(RAD(90),0,0)).lookVector local Y=-u.lookVector local p=(q*CFANG(RAD(90),0,0)).lookVector local t=-q.lookVector local j=u.p local J=q.p local w=e(j:lerp(J,h),S:lerp(p,h),Y:lerp(t,h))return w end local J=u.C0 local w=u.C1 local D=0 while true do local e=D+math.min(1.5/math.max(Y,0),90)D=e>90 and 90 or e if p.Value~=t then break end if not Selected then break end if q then u.C0=j(J,q,S(D))end if h then u.C1=j(w,h,S(D))end if D==90 then break end RS:wait()end if p.Value==t then p:Destroy()end end))end function RotCamera(u,q,h,S)spawn(function()if h then local h=nil local Y=math.random(-1000000000.0,1000000000.0)if not Camera:FindFirstChild("\084\119\101\101\110\067\111\100\101")then h=Instance.new("\073\110\116\086\097\108\117\101")h.Name="\084\119\101\101\110\067\111\100\101"h.Value=Y h.Parent=Camera else h=Camera.TweenCode h.Value=Y end local p=math.min(1.5/math.max(S,0),90)local t=0 while true do local S=t+p t=S>90 and 90 or S if h.Value~=Y then break end if not Selected then break end local e=Camera.CoordinateFrame-Camera.CoordinateFrame.p local j=(Camera.CoordinateFrame.p-Camera.Focus.p).magnitude local J=(CF(Camera.Focus.p)*e)*CFANG(u/(90/p),q/(90/p),0)Camera.CoordinateFrame=CF(J.p,J.p+J.lookVector)*CF(0,0,j)if t==90 then break end RS:wait()end if h.Value==Y then h:Destroy()end else local h=Camera.CoordinateFrame-Camera.CoordinateFrame.p local S=(Camera.CoordinateFrame.p-Camera.Focus.p).magnitude local Y=(CF(Camera.Focus.p)*h)*CFANG(u,q,0)Camera.CoordinateFrame=CF(Y.p,Y.p+Y.lookVector)*CF(0,0,S)end end)end
--------------------[ TWEEN FUNCTIONS ]-----------------------------------------------

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
