local w=2.8 local function n(w,n,a)if w<=n then return math.min(n,math.max(w,a))end return a end local function a(w,n)n=n or 0 local a=10^n return math.floor(w*a+.5)/a end local function g()local g={}local Q=tick()local I=false local C=false local W=nil local P,V=nil,nil local M={}local m={}local k={}local function G(w)if w.Parent==nil then return false end return w.Parent:IsA("\084\111\111\108")or G(w.Parent)end local function f(w)if w:IsA("\066\097\115\101\080\097\114\116")or w:IsA("\068\101\099\097\108")then return not G(w)end return false end local function p(w)if w then if f(w)then k[w]=true I=true end for w,n in pairs(w:GetChildren())do p(n)end end end local function t()for w,n in pairs(k)do w.LocalTransparencyModifier=0 end k={}I=true W=nil if P then P:disconnect()P=nil end if V then V:disconnect()V=nil end for w,n in pairs(M)do n:disconnect()M[w]=nil end for w,n in pairs(m)do n:disconnect()m[w]=nil end end local function d(w)t()if P then P:disconnect()end P=w.DescendantAdded:connect(function(n)if f(n)then k[n]=true I=true elseif n:IsA("\084\111\111\108")then if M[n]then M[n]:disconnect()end M[n]=n.DescendantAdded:connect(function(w)k[w]=nil if w:IsA("\066\097\115\101\080\097\114\116")or w:IsA("\068\101\099\097\108")then w.LocalTransparencyModifier=0 end end)if m[n]then m[n]:disconnect()end m[n]=n.DescendantRemoving:connect(function(n)wait()if w and(n and n:IsDescendantOf(w))then if f(n)then k[n]=true I=true end end end)end end)if V then V:disconnect()end V=w.DescendantRemoving:connect(function(w)if k[w]then k[w]=nil w.LocalTransparencyModifier=0 end end)p(w)end function g.SetEnabled(n,w)if C~=w then C=w n:Update()end end function g.SetSubject(n,w)local a=nil if w and w:IsA("\072\117\109\097\110\111\105\100")then a=w.Parent end if w and(w:IsA("\086\101\104\105\099\108\101\083\101\097\116")and w.Occupant)then a=w.Occupant.Parent end if a then d(a)else t()end end function g.Update(g)local P=false local V=tick()local M=workspace.CurrentCamera if M then local g=0 if not C then P=true else local C=(M.Focus.p-M.CoordinateFrame.p).magnitude g=(7-C)/5 if g<.5 then g=0 end if W then local a=g-W if not P and(g<1 and W<.95)then local g=w*(V-Q)a=n(-g,g,a)end g=W+a else I=true end g=n(0,1,a(g,2))end if I or W~=g then for w,n in pairs(k)do w.LocalTransparencyModifier=g end I=false W=g end end Q=V end return g end return g
-- SolarCrane

local MAX_TWEEN_RATE = 2.8 -- per second

local function clamp(low, high, num)
	if low <= high then
		return math.min(high, math.max(low, num))
	end
	return num
end

local function Round(num, places)
	places = places or 0
	local decimalPivot = 10 ^ places
	return math.floor(num * decimalPivot + 0.5) / decimalPivot
end

local function CreateTransparencyController()
	local module = {}

	local LastUpdate = tick()
	local TransparencyDirty = false
	local Enabled = false
	local LastTransparency = nil

	local DescendantAddedConn, DescendantRemovingConn = nil, nil
	local ToolDescendantAddedConns = {}
	local ToolDescendantRemovingConns = {}
	local CachedParts = {}

	local function HasToolAncestor(object)
		if object.Parent == nil then
			return false
		end
		return object.Parent:IsA("Tool") or HasToolAncestor(object.Parent)
	end

	local function IsValidPartToModify(part)
		if part:IsA("BasePart") or part:IsA("Decal") then
			return not HasToolAncestor(part)
		end
		return false
	end

	local function CachePartsRecursive(object)
		if object then
			if IsValidPartToModify(object) then
				CachedParts[object] = true
				TransparencyDirty = true
			end
			for _, child in pairs(object:GetChildren()) do
				CachePartsRecursive(child)
			end
		end
	end

	local function TeardownTransparency()
		for child, _ in pairs(CachedParts) do
			child.LocalTransparencyModifier = 0
		end
		CachedParts = {}
		TransparencyDirty = true
		LastTransparency = nil

		if DescendantAddedConn then
			DescendantAddedConn:disconnect()
			DescendantAddedConn = nil
		end
		if DescendantRemovingConn then
			DescendantRemovingConn:disconnect()
			DescendantRemovingConn = nil
		end
		for object, conn in pairs(ToolDescendantAddedConns) do
			conn:disconnect()
			ToolDescendantAddedConns[object] = nil
		end
		for object, conn in pairs(ToolDescendantRemovingConns) do
			conn:disconnect()
			ToolDescendantRemovingConns[object] = nil
		end
	end

	local function SetupTransparency(character)
		TeardownTransparency()

		if DescendantAddedConn then
			DescendantAddedConn:disconnect()
		end
		DescendantAddedConn = character.DescendantAdded:connect(function(object)
			-- This is a part we want to invisify
			if IsValidPartToModify(object) then
				CachedParts[object] = true
				TransparencyDirty = true
			-- There is now a tool under the character
			elseif object:IsA("Tool") then
				if ToolDescendantAddedConns[object] then
					ToolDescendantAddedConns[object]:disconnect()
				end
				ToolDescendantAddedConns[object] = object.DescendantAdded:connect(function(toolChild)
					CachedParts[toolChild] = nil
					if toolChild:IsA("BasePart") or toolChild:IsA("Decal") then
						-- Reset the transparency
						toolChild.LocalTransparencyModifier = 0
					end
				end)
				if ToolDescendantRemovingConns[object] then
					ToolDescendantRemovingConns[object]:disconnect()
				end
				ToolDescendantRemovingConns[object] = object.DescendantRemoving:connect(function(formerToolChild)
					wait() -- wait for new parent
					if character and formerToolChild and formerToolChild:IsDescendantOf(character) then
						if IsValidPartToModify(formerToolChild) then
							CachedParts[formerToolChild] = true
							TransparencyDirty = true
						end
					end
				end)
			end
		end)
		if DescendantRemovingConn then
			DescendantRemovingConn:disconnect()
		end
		DescendantRemovingConn = character.DescendantRemoving:connect(function(object)
			if CachedParts[object] then
				CachedParts[object] = nil
				-- Reset the transparency
				object.LocalTransparencyModifier = 0
			end
		end)
		CachePartsRecursive(character)
	end

	function module:SetEnabled(newState)
		if Enabled ~= newState then
			Enabled = newState
			self:Update()
		end
	end

	function module:SetSubject(subject)
		local character = nil
		if subject and subject:IsA("Humanoid") then
			character = subject.Parent
		end
		if subject and subject:IsA("VehicleSeat") and subject.Occupant then
			character = subject.Occupant.Parent
		end
		if character then
			SetupTransparency(character)
		else
			TeardownTransparency()
		end
	end

	function module:Update()
		local instant = false
		local now = tick()
		local currentCamera = workspace.CurrentCamera

		if currentCamera then
			local transparency = 0
			if not Enabled then
				instant = true
			else
				local distance = (currentCamera.Focus.p - currentCamera.CoordinateFrame.p).magnitude
				transparency = (7 - distance) / 5
				if transparency < 0.5 then
					transparency = 0
				end

				if LastTransparency then
					local deltaTransparency = transparency - LastTransparency
					-- Don't tween transparency if it is instant or your character was fully invisible last frame
					if not instant and transparency < 1 and LastTransparency < 0.95 then
						local maxDelta = MAX_TWEEN_RATE * (now - LastUpdate)
						deltaTransparency = clamp(-maxDelta, maxDelta, deltaTransparency)
					end
					transparency = LastTransparency + deltaTransparency
				else
					TransparencyDirty = true
				end

				transparency = clamp(0, 1, Round(transparency, 2))
			end

			if TransparencyDirty or LastTransparency ~= transparency then
				for child, _ in pairs(CachedParts) do
					child.LocalTransparencyModifier = transparency
				end
				TransparencyDirty = false
				LastTransparency = transparency
			end
		end
		LastUpdate = now
	end

	return module
end

return CreateTransparencyController
