local g=2.8 local function e(g,e,V)if g<=e then return math.min(e,math.max(g,V))end return V end local function V(g,e)e=e or 0 local V=10^e return math.floor(g*V+.5)/V end local function y()local y={}local b=tick()local p=false local M=false local E=nil local u,I=nil,nil local B={}local k={}local h={}local function Y(g)if g.Parent==nil then return false end return g.Parent:IsA("\084\111\111\108")or Y(g.Parent)end local function v(g)if g:IsA("\066\097\115\101\080\097\114\116")or g:IsA("\068\101\099\097\108")then return not Y(g)end return false end local function Z(g)if g then if v(g)then h[g]=true p=true end for g,e in pairs(g:GetChildren())do Z(e)end end end local function n()for g,e in pairs(h)do g.LocalTransparencyModifier=0 end h={}p=true E=nil if u then u:disconnect()u=nil end if I then I:disconnect()I=nil end for g,e in pairs(B)do e:disconnect()B[g]=nil end for g,e in pairs(k)do e:disconnect()k[g]=nil end end local function T(g)n()if u then u:disconnect()end u=g.DescendantAdded:connect(function(e)if v(e)then h[e]=true p=true elseif e:IsA("\084\111\111\108")then if B[e]then B[e]:disconnect()end B[e]=e.DescendantAdded:connect(function(g)h[g]=nil if g:IsA("\066\097\115\101\080\097\114\116")or g:IsA("\068\101\099\097\108")then g.LocalTransparencyModifier=0 end end)if k[e]then k[e]:disconnect()end k[e]=e.DescendantRemoving:connect(function(e)wait()if g and(e and e:IsDescendantOf(g))then if v(e)then h[e]=true p=true end end end)end end)if I then I:disconnect()end I=g.DescendantRemoving:connect(function(g)if h[g]then h[g]=nil g.LocalTransparencyModifier=0 end end)Z(g)end function y.SetEnabled(e,g)if M~=g then M=g e:Update()end end function y.SetSubject(e,g)local V=g and(g:IsA("\072\117\109\097\110\111\105\100")and g.Parent)if V then T(V)else n()end end function y.Update(y)local u=false local I=tick()local B=workspace.CurrentCamera if B then local y=0 if not M then u=true else local M=(B.Focus.p-B.CoordinateFrame.p).magnitude y=(7-M)/5 if y<.5 then y=0 end if E then local V=y-E if not u and(y<1 and E<.95)then local y=g*(I-b)V=e(-y,y,V)end y=E+V else p=true end y=e(0,1,V(y,2))end if p or E~=y then for g,e in pairs(h)do g.LocalTransparencyModifier=y end p=false E=y end end b=I end return y end return y
-- 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 = subject and subject:IsA("Humanoid") and subject.Parent
		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
